words <- c("word1", "word")
text <- c("this is word1", "this is word2", "this is word4")
如果我使用sapply(words, grepl, text)
让你真假回答的列表, 代替我怎样让这个已经匹配了确切的话 使得答案将是
"this is word1"
我对R这个愚蠢的问题是新来的。 欢迎任何想法。
words <- c("word1", "word")
text <- c("this is word1", "this is word2", "this is word4")
如果我使用sapply(words, grepl, text)
让你真假回答的列表, 代替我怎样让这个已经匹配了确切的话 使得答案将是
"this is word1"
我对R这个愚蠢的问题是新来的。 欢迎任何想法。
一种选择是创建字边界,然后使用grep
避免串并与value = TRUE
任何部分匹配,它返回的字符串,而不是指数
grep(paste0("\\b(", paste(words, collapse="|"), ")\\b"), text, value = TRUE)
#[1] "this is word1"
可以检查word =“test” text = c(“test foo”,“word 1”,“word2”) grep(paste0(“\\ b(”,paste(word,collapse =“| “),”)\\ b“),text,value = TRUE)。它为我提供了”test foo“,但不希望它给我没有答案或null(因为没有找到相位)。 – Domnick
@Domnick你的情况对我来说并不明确。如果你想要一个精确的匹配,那么使用'=='或'%in%'即'哪个(word ==文本)' – akrun
好吧。只是出于好奇才有可能做反之亦然,这意味着如果单词匹配,从变量*单词*中得到匹配的单词? – Domnick
'sapply(也就是说,grep的,文本,值= TRUE)'? – Jaap