How to select groups based on a condition for individual rows, say, filter all groups that contain a value of 4 (or any other condition).
Let's take very simple data with two groups, and I want to select group B (as it matters 4)
library(dplyr) df <- data.frame(Group=LETTERS[c(1,1,1,2,2,2)], Value=c(1:5,4)) > df Group Value 1 A 1 2 A 2 3 B 3 4 B 4
Executing group_by() and then filter (as in this post ) will select only individual rows containing the value 4, and not the entire group:
df %>% group_by(Group) %>% filter(Value==4) Group Value <fctr> <int> 1 B 4
source share