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

How to filter every row of the same character if one attend the condition

Suppose I have the following dataset test

> test = data.frame(location = c("here", "there", "here", "there", "where"), x = 1:5, y = c(6,7,8,6,10))
> test
  location x  y
1     here 1  6
2    there 2  7
3     here 3  8
4    there 4  6
5    where 5 10

Then, I want to make a condition where if y satisfy a condition, every location matched once are maintained in the dataset, something like

test %>% filter_something(y == 6)
  location x  y
1     here 1  6
2    there 2  7
3     here 3  8
4    there 4  6

Note that, even in line 4 there is no y = 6, they keep on the dataset, since there is at least one case where location match the ‘right’ y.

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

I can solve this problem creating another dataset using y == 6, and then doing an inner join with test, but any hint if there is another option more elegant?, because I’m not filtering just this variable, but I’m using another columns too.

>Solution :

We can group_by location, then use any(condition)

library(dplyr)

test %>% group_by(location) %>%
         filter(any(y==6))
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