regex - 从没有语料库的数据框中提取子词列表

标签 regex r dataframe

实际上我想在数据框中提取一些子词列表,我知道我们可以通过语料库提取但我不想这样做不必要的。首先我使用了 ma​​tch 以及 grep 但问题是除了完全匹配之外不能使用匹配,而且 grep 不能用于多个单词。例如。

 a=sample(c("Client","offshor","V1fax","12mobile"),10)
 z=data.frame(a)
 z
          a
1     V1fax
2     V1fax
3  12mobile
4  12mobile
5     V1fax
6     clint
7   offshor
8     clint
9     clint
10 12mobile

d=z[is.na(match(tolower(z[,1]),c("fax","mobile","except","talwade"))),]

grep(c("fax","mobile","except","talwade"),tolower(z[,1]))
    [1] 1 2 5
Warning message:
In grep(c("fax", "mobile", "except", "talwade"  :
  argument 'pattern' has length > 1 and only the first element will be used

想要一个o/p成为

z
       a
1     clint
2   offshor
3     clint
4     clint

正如预期的那样,任何提取子词列表的有效方法。谢谢。

最佳答案

你可以用 grep 来做,你只需要使用正则表达式 OR 运算符,它是 |...

grep(  paste( c("fax","mobile","except","talwade") , collapse = "|" ) , tolower(z[,1]) )
# [1] 1 2 3 4 5 10


#  The pattern...
paste( c("fax","mobile","except","talwade") , collapse = "|" )
# [1] "fax|mobile|except|talwade"

关于regex - 从没有语料库的数据框中提取子词列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20540224/

相关文章:

javascript - 匹配 0 到 9 和选定符号的正则表达式

r - 在多个列中搜索以多个字符串中的任何一个开头的列;得到计数

java - 无数字 Java 正则表达式模式

r - 通过 Shiny 在布局之间进行响应式(Reactive)切换

python - DatetimeIndex 阻止 DataFrame 从装饰函数返回

python - 拆分两行组内给定列的差异

regex - 如何在 Perl 的正则表达式中使用 chr 函数?

r - 如何对因子水平进行分组?

r - RSelenium 和 ChromeDriver 的问题 - "This version of ChromeDriver only supports Chrome version X"

python - 如何获取 pandas 中出现频率较低的行的索引