Delete rows containing specific strings in R

2019-01-06 12:10发布

I would like to exclude lines containing a string "REVERSE", but my lines do not match exactly with the word, just contain it.

My input data frame:

   Value   Name 
    55     REVERSE223   
    22     GENJJS
    33     REVERSE456
    44     GENJKI

My expected output:

   Value   Name 
    22     GENJJS
    44     GENJKI

4条回答
▲ chillily
2楼-- · 2019-01-06 12:28

You could use dplyr::filter() and negate a grepl() match:

library(dplyr)

df %>% 
  filter(!grepl('REVERSE', Name))

Or with dplyr::filter() and negating a stringr::str_detect() match:

library(stringr)

df %>% 
  filter(!str_detect(Name, 'REVERSE'))
查看更多
贪生不怕死
3楼-- · 2019-01-06 12:31

This should do the trick:

df[- grep("REVERSE", df$Name),]

Or a safer version would be:

df[!grepl("REVERSE", df$Name),]
查看更多
三岁会撩人
4楼-- · 2019-01-06 12:32

Actually I would use:

df[ grep("REVERSE", df$Name, invert = TRUE) , ]

This will avoid deleting all of the records if the desired search word is not contained in any of the rows.

查看更多
5楼-- · 2019-01-06 12:39

You can use stri_detect_fixed function from stringi package

stri_detect_fixed(c("REVERSE223","GENJJS"),"REVERSE")
[1]  TRUE FALSE
查看更多
登录 后发表回答