2

Suppose we have a data frame with a grouping column and a values column. E.g.,

test <- data.frame(Group = rep(c("A", "B", "C"), 3), Value = c(1, 10, 2, 4, 4, 4, 6, 6, 6))

What's the best way to get a vector of unique values which appear in all group, i.e., values common to all groups?

Here's a clumsy way (using dplyr):

test %>% 
 group_by(Value) %>% 
 mutate(In_groups = length(unique(Group))) %>% 
 ungroup() %>% 
 filter(In_groups == length(unique(Group))) %>% 
 .$Value %>% 
 unique()

It seems like there should be a better way to do this.

Victor Kostyuk
  • 621
  • 5
  • 16

0 Answers0