简体   繁体   中英

Select rows with minimum number of occurrence of value by group

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM