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))