这应该适合你:
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"))