Remove groups that contain certain strings

前端 未结 4 2053
孤城傲影
孤城傲影 2021-01-19 15:15

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 p

相关标签:
4条回答
  • 2021-01-19 15:22

    Another option could be using %in% operator.

    df %>% 
     filter(!(gr %in% unique(ifelse(grepl("\\.",vals),gr,NA) )))
    
    #  vals gr
    #1 good  1
    #2  bad  1
    #3 ugly  1
    
    0 讨论(0)
  • 2021-01-19 15:31

    Here is one option in base R with subset and table

    subset(df, gr %in% names(which(!table(gr, grepl("\\.", vals))[,2])))
    #  vals gr
    #1 good  1
    #2  bad  1
    #3 ugly  1
    
    0 讨论(0)
  • 2021-01-19 15:34

    Maybe something like this:

    df %>% group_by(gr) %>% filter(all(!grepl("\\.",vals)))
    
    0 讨论(0)
  • 2021-01-19 15:42

    The OP has requested to remove the entire group when one of the group members contains a certain string in vals - without breaking the pipe.

    The OP explicitely has stated: I mean without using any join function.

    However, I believe using an anti-join does not break the pipe:

    library(dplyr)
    data.frame(vals, gr) %>% 
      anti_join(., filter(., grepl("\\.",vals)), by = "gr")
    
      vals gr
    1 good  1
    2  bad  1
    3 ugly  1
    
    0 讨论(0)
提交回复
热议问题