2

Similar to this question but with an added wrinkle. I'd like to filter only groups of rows that have both of two (or all of several) values in a particular column in any row of the group.

For example, say I have this dataframe:

df <- data.frame(Group=LETTERS[c(1,1,1,2,2,2,3,3,3,3)], Value=c(5, 1:4, 1:4, 5))

And I want those letters where any letter has both a row with a corresponding value of 4 AND a row with a corresponding value of 5, so this:

  Group Value
  <fct> <dbl>
1 C         2
2 C         3
3 C         4
4 C         5

I can do that with a pair of any calls inside filter like this:

df %>% 
  group_by(Group) %>% 
  filter(any(Value == 4),
         any(Value == 5))

Is there a way to do the filter call in one line? Something like: (note this doesn't work, all_of is not a real function)

df %>% 
  group_by(Group) %>% 
  filter(all_of(Value == 4 & Value == 5))
Greg
  • 3,570
  • 5
  • 18
  • 31

1 Answers1

6

all is a valid function and can be used in combination with %in% (for vectors of length >=1)

library(dplyr)
df %>% 
    group_by(Group) %>%
    filter(all(c(4, 5) %in% Value))
# A tibble: 4 x 2
# Groups:   Group [1]
#  Group Value
#  <fct> <dbl>
#1 C         2
#2 C         3
#3 C         4
#4 C         5

Or with sum of logical vector

df %>%
    group_by(Group) %>%
    filter(!sum(!c(4, 5) %in% Value))
akrun
  • 874,273
  • 37
  • 540
  • 662