R 从字符串中删除包含特定值的所有行
如何从包含我在字符串向量中指定的任何值的数据帧中删除所有行?我试过grepl,但这似乎只有在涉及一个词时才起作用R 从字符串中删除包含特定值的所有行,r,match,rows,grepl,R,Match,Rows,Grepl,如何从包含我在字符串向量中指定的任何值的数据帧中删除所有行?我试过grepl,但这似乎只有在涉及一个词时才起作用 number <- c(1:5) text <- c("First text","Second text","Another text here","Yet another one","Last text") example <- as.data.frame(cbind(number,text)) number text 1
number <- c(1:5)
text <- c("First text","Second text","Another text here","Yet another one","Last text")
example <- as.data.frame(cbind(number,text))
number text
1 1 First text
2 2 Second text
3 3 Another text here
4 4 Yet another one
5 5 Last text
我们可以
将“remove”中的元素粘贴到一个由|分隔的字符串中(意思是或),并在grepl
中将其作为模式提供给“text”列,对逻辑向量求反,然后对“example”行进行子集
example[!grepl(paste(remove, collapse="|"), example$text),]
# number text
#2 2 Second text
#3 3 Another text here
#5 5 Last text
工作完美。谢谢@Jaap此问题与其他哪个问题重复?请link@abhishah901请看问题的顶部。。。。。
number text
1 2 Second text
2 3 Another text here
3 5 Last text
example[!grepl(paste(remove, collapse="|"), example$text),]
# number text
#2 2 Second text
#3 3 Another text here
#5 5 Last text