Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 values 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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Expected:

  File  value
  <chr> <dbl>
1 A     1.11 
2 A     1.22 
3 A     0.001
4 A     0.999

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading