【问题标题】:Remove all rows containing certain values from a string [duplicate]从字符串中删除包含某些值的所有行[重复]
【发布时间】:2016-04-12 11:56:17
【问题描述】:

如何从包含我在字符串向量中分配的任何值的数据框中删除所有行?我试过 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      1        First text
2      2       Second text
3      3 Another text here
4      4   Yet another one
5      5         Last text

不起作用:

remove <- c("First","Yet")
example[!grepl(remove,example$text),]

期望的结果:

 number              text
1      2       Second text
2      3 Another text here
3      5         Last text

【问题讨论】:

  • @Jaap 这个问题与其他哪个问题重复?请链接
  • @abhishah901 看到问题的顶部.....

标签: r match rows grepl


【解决方案1】:

我们可以paste 'remove' 中的元素到由| 分隔的单个字符串(这意味着OR)并将其作为pattern 在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

【讨论】:

  • 完美运行。谢谢!
猜你喜欢
  • 2020-09-11
  • 2018-08-08
  • 2021-04-07
  • 2020-03-19
  • 2021-04-28
  • 2014-03-26
  • 2019-09-14
  • 1970-01-01
  • 2016-07-16
相关资源
最近更新 更多