【发布时间】:2017-08-02 21:44:50
【问题描述】:
我想检查一组“关键字”中的 any 是否出现在字符串中。因此,对于下面的 "text",结果应该是 TRUE(或 1),而对于 text_2,结果应该是 FALSE(或 0)。
keywords <- c("one", "two", "three", "four") #set of keywords
text <- "Blah blah one blah two"
text_2 <- "Blah blah"
我尝试了一些关于 str_detect 的变体,但我卡住了。
例如,我知道我没有正确使用此功能,但是:
> keywords <- c("motor", "car", "ford") #list of keywords
> text <- "I am looking to buy a ford" #string I'd like to check
> ifelse(str_detect(text, pattern = keywords), 1, 0)
[1] 0 0 1
有更好的方法吗?
【问题讨论】: