实际上我想在数据框中提取一些子词列表,我知道我们可以通过语料库提取但我不想这样做不必要的。首先我使用了 match 以及 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/