简体   繁体   中英

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

We could use n_distinct for filter ing 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM