Delete groups containing specific rows.

I have a problem with deleting groups that contain certain lines in its lines, for example, if it includes .. I would like to achieve this without breaking the assembly line. I mean without using any function join.

Sample 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 deletes only the line matching the condition. But I want to remove the integers gr 2.

 vals gr
1 good  1
2  bad  1
3 ugly  1
4 good  2
5 ugly  2
+4
source share
4 answers

Maybe something like this:

df %>% group_by(gr) %>% filter(all(!grepl("\\.",vals)))
+8
source

Another option would be to use an operator %in%.

df %>% 
 filter(!(gr %in% unique(ifelse(grepl("\\.",vals),gr,NA) )))

#  vals gr
#1 good  1
#2  bad  1
#3 ugly  1
+2
source

OP , vals - .

The OP explicitly states: I mean without using any function join.

However, I believe that using an anti-connection does not disrupt the channel:

library(dplyr)
data.frame(vals, gr) %>% 
  anti_join(., filter(., grepl("\\.",vals)), by = "gr")
  vals gr
1 good  1
2  bad  1
3 ugly  1
+2
source

Here is one of the options in base Rs subsetandtable

subset(df, gr %in% names(which(!table(gr, grepl("\\.", vals))[,2])))
#  vals gr
#1 good  1
#2  bad  1
#3 ugly  1
+1
source

Source: https://habr.com/ru/post/1693949/


All Articles