string 在 R 中删除包含特定字符串的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22249702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:14:11  来源:igfitidea点击:

Delete rows containing specific strings in R

rstringmatchrows

提问by user3091668

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

我想排除包含字符串“REVERSE”的行,但我的行与单词不完全匹配,只包含它。

My input data frame:

我的输入数据框:

   Value   Name 
    55     REVERSE223   
    22     GENJJS
    33     REVERSE456
    44     GENJKI

My expected output:

我的预期输出:

   Value   Name 
    22     GENJJS
    44     GENJKI

回答by Pop

This should do the trick:

这应该可以解决问题:

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

Or a safer version would be:

或者更安全的版本是:

df[!grepl("REVERSE", df$Name),]

回答by BobD59

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.

如果所需的搜索词不包含在任何行中,这将避免删除所有记录。

回答by sbha

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

您可以使用dplyr::filter()和否定grepl()匹配:

library(dplyr)

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

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

或者使用dplyr::filter()和否定stringr::str_detect()匹配:

library(stringr)

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

回答by bartektartanus

You can use stri_detect_fixed function from stringipackage

您可以使用stringi包中的stri_detect_fixed 函数

stri_detect_fixed(c("REVERSE223","GENJJS"),"REVERSE")
[1]  TRUE FALSE

回答by Mohamed Rahouma

You can use it in the same datafram (df) using the previously provided code

您可以使用之前提供的代码在同一个数据帧 (df) 中使用它

df[!grepl("REVERSE", df$Name),]

or you might assign a different name to the datafram using this code

或者您可以使用此代码为数据帧分配不同的名称

df1<-df[!grepl("REVERSE", df$Name),]