-1
我期待着一个R解决方案,它可以检查句子中是否存在单词或句子(列1中)(在第2列中)是否有数据帧。如果单词/单词出现在句子中,则它应该返回1(TRUE)或0(FALSE)。 和如何找到一列中的单词/单词出现在另一列中构成一个句子
我期待着一个R解决方案,它可以检查句子中是否存在单词或句子(列1中)(在第2列中)是否有数据帧。如果单词/单词出现在句子中,则它应该返回1(TRUE)或0(FALSE)。 和如何找到一列中的单词/单词出现在另一列中构成一个句子
这应该为你工作:
df[, "lookup"] <- gsub(" ", "|", df[,"substring"])
df[,"t"] <- mapply(grepl, df[,"lookup"], df[,"string"])
df
# substring string lookup t
#1 my new phone this is a mobile phone my|new|phone TRUE
#2 She would buy new phones Yes, I have two phones She|would|buy|new|phones TRUE
#3 telephonessss my old telephone telephonessss FALSE
#4 telephone234 telephone234 telephone234 TRUE
你可以得到与创建查找列更花哨,但这种情况下没有必要,所以我用一个简单的gsub
。
数据:
df <- data.frame(substring = c("my new phone", "She would buy new phones", "telephonessss", "telephone234"),
string = c("this is a mobile phone", "Yes, I have two phones", "my old telephone", "telephone234"))
或者使用dplyr
& stringr
解决方案。但原则上其同样的想法:
library(tidyverse)
library(stringr)
df %>%
mutate(result=str_detect(df$string,gsub(" ", "|", df$substring)))
substring string result
1 my new phone this is a mobile phone TRUE
2 She would buy new phones Yes, I have two phones TRUE
3 telephonessss my old telephone FALSE
4 telephone234 telephone234 TRUE
使用本:https://stackoverflow.com/questions/26319567/use-grepl-to-search-either-of-multiple-substrings-in-a-text -in-r like'grepl(gsub(“”,“|”,“my new phone”),“这是我的手机”) – Jimbou
也许'mapply'和'grepl'? –
我尝试了mapply()和grepl(),但它们只是将子字符串(column1)的第一个字与字符串(column2)进行比较。 – Fraxxx