我有一个术语向量和一些文本
terms <- c("this","that","those")
text <- c("this is some text","here is more text with those words","more text than that other one","this ends it")我想使用grepl搜索所有文本中的所有术语,并按by文本返回T/F。我试过了
sapply(text,grepl,pattern = terms)但这只是给出了第一学期的答案
sapply(text,grepl,terms)这是有效的,但没有给出正确的答案(每个词返回为假(没有出现在任何文本)。
sapply(text,grepl,sapply(terms,'['))这也不起作用,并返回错误的答案(都是假的)。
发布于 2022-02-03 16:59:49
你们关系很好。当您使用sapply时,pattern=必须长度为1,因此您应该在sapply中添加x=text
out <- sapply(terms, grepl, x = text)
out
# this that those
# [1,] TRUE FALSE FALSE
# [2,] FALSE FALSE TRUE
# [3,] FALSE TRUE FALSE
# [4,] TRUE FALSE FALSE如果需要知道是否匹配,可以使用rowSums或colSums
colSums(out) > 0
# this that those
# TRUE TRUE TRUE
setNames(rowSums(out) > 0, nm = text)
# this is some text here is more text with those words more text than that other one
# TRUE TRUE TRUE
# this ends it
# TRUE ( setNames纯粹是为了确定哪个逻辑是哪个text的,以防这是您想要的方向。)
https://stackoverflow.com/questions/70975282
复制相似问题