Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove groups that contain certain strings

Tags:

r

dplyr

I have an issue about removing the groups that contain certain strings in its rows for example if includes .. I would like to achive this without breaking the pipeline. I mean without using any join function.

The example data

vals <- c("good","bad",'ugly',"good","bad.","ugly")

    gr <- gl(2,3)

vals gr
1 good  1
2  bad  1
3 ugly  1
4 good  2
5 bad.  2
6 ugly  2

df <- data.frame(vals,gr)

I tried

library(dplyr)
        df%>%
          filter(!grepl("\\.",vals))

which removes only the row that match the condition. But I want to remove entire gr 2.

 vals gr
1 good  1
2  bad  1
3 ugly  1
4 good  2
5 ugly  2
like image 661
Alexander Avatar asked Dec 10 '22 07:12

Alexander


1 Answers

Maybe something like this:

df %>% group_by(gr) %>% filter(all(!grepl("\\.",vals)))
like image 199
joran Avatar answered Dec 21 '22 23:12

joran