我有一个很大的data.table,每一行都有一行文本。我试图通过查找包含多个单词之一的行来对data.table进行子集化。这是我尝试过的。
textDt <- data.table(LinesOfText = c("There was a small frog.","Most of the
time I ate chicken","There are so many places to stay here.","People on
stackoverflow are tremendously helpful.","Why do grapefuits cause weird drug
interactions?","If I were tiny I could fit in there"))
targetWords <- c("small","tiny","no room","cramped","mini")
targetDt <- textDt[targetWords %in% LinesOfText]
这总是会导致错误。我知道必须有一个简单的解决方案使我望而却步。
我喜欢使用,stringr
因为我相信它会更快。因此,这是基于此的解决方案:
library(stringr)
targetWords<- paste(targetWords, collapse = "|")
# "small|tiny|no room|cramped|mini"
targetDT<- textDt[str_detect(LinesOfText , targetWords)]
targetDT
# LinesOfText
#1: If I were tiny I could fit in there
#2: There was a small frog.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句