In this dataframe:
df <- data.frame(
File = c("A", "A", "A", "A", "B", "B", "B", "C", "C", "C"),
value = c(1.111, 1.222, 0.001, 0.999, 0.12, 1.23, 0.000, 0.55, 0.666, 0.666))
I want to select those rows where at least two value
s are > 1
per group. I know how to select those rows where at least one value
is > 1
per group:
library(dplyr)
df %>%
group_by(File) %>%
filter(any(value > 1))
How do I have to adapt this to filter as specified above?
Expected:
File value
<chr> <dbl>
1 A 1.11
2 A 1.22
3 A 0.001
4 A 0.999
CodePudding user response:
filter(sum(value > 1) > 1)
. sum(value >1)
counts how many values are greater than 1, and the second > 1
keeps groups that have 2 or more values greater than 1.
library(dplyr)
df %>%
group_by(File) %>%
filter(sum(value > 1) > 1)