Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Grouped filter common value in a column

Sample data:

# A tibble: 10 × 2
      id value
   <int> <dbl>
 1     1     1
 2     1     2
 3     1     3
 4     1     5
 5     1     6
 6     2     6
 7     2     3
 8     2     2
 9     2     0
10     2    10

structure(list(id = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), 
    value = c(1, 2, 3, 5, 6, 6, 3, 2, 0, 10)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -10L))

How do I perform a group filter for common values in the column value with dplyr? Such that the expected output would be:

# A tibble: 6 × 2
# Groups:   id [2]
     id value
  <int> <dbl>
1     1     2
2     1     3
3     1     6
4     2     6
5     2     3
6     2     2

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We could use n_distinct for filtering after grouping by ‘value’

library(dplyr)
df1 %>% 
   group_by(value) %>%
   filter(n_distinct(id) == n_distinct(df1$id)) %>% 
   ungroup

-output

# A tibble: 6 × 2
     id value
  <int> <dbl>
1     1     2
2     1     3
3     1     6
4     2     6
5     2     3
6     2     2

Or use split/reduce/intersect

library(purrr)
df1 %>%
    filter(value %in% (split(value, id) %>% reduce(intersect)))

-output

# A tibble: 6 × 2
     id value
  <int> <dbl>
1     1     2
2     1     3
3     1     6
4     2     6
5     2     3
6     2     2

In base R, it would be

subset(df1, value %in% Reduce(intersect, split(value, id)))

-output

# A tibble: 6 × 2
     id value
  <int> <dbl>
1     1     2
2     1     3
3     1     6
4     2     6
5     2     3
6     2     2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading