use*_*668 46 string r rows match
我想排除包含字符串"REVERSE"的行,但我的行与单词完全不匹配,只包含它.
我的输入数据框:
Value Name
55 REVERSE223
22 GENJJS
33 REVERSE456
44 GENJKI
Run Code Online (Sandbox Code Playgroud)
我的预期产量:
Value Name
22 GENJJS
44 GENJKI
Run Code Online (Sandbox Code Playgroud)
Pop*_*Pop 73
这应该做的伎俩:
df[- grep("REVERSE", df$Name),]
Run Code Online (Sandbox Code Playgroud)
或者更安全的版本是:
df[!grepl("REVERSE", df$Name),]
Run Code Online (Sandbox Code Playgroud)
小智 17
其实我会用:
df[ grep("REVERSE", df$Name, invert = TRUE) , ]
Run Code Online (Sandbox Code Playgroud)
如果所需的搜索词未包含在任何行中,这将避免删除所有记录.
您可以使用stringi包中的stri_detect_fixed 函数
stri_detect_fixed(c("REVERSE223","GENJJS"),"REVERSE")
[1] TRUE FALSE
Run Code Online (Sandbox Code Playgroud)
您可以使用dplyr::filter()和取消grepl()匹配:
library(dplyr)
df %>%
filter(!grepl('REVERSE', Name))
Run Code Online (Sandbox Code Playgroud)
或与dplyr::filter()否定stringr::str_detect()匹配项:
library(stringr)
df %>%
filter(!str_detect(Name, 'REVERSE'))
Run Code Online (Sandbox Code Playgroud)