Home > Back-end >  Grouped filter common value in a column
Grouped filter common value in a column

Time:12-23

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

CodePudding user response:

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
  • Related