【发布时间】:2021-05-08 13:38:44
【问题描述】:
您好,我有一个文本,我想只检索包含某些单词的句子。这是一个例子。
my_text<- tolower(c("Pro is a molecule that can be found in the air. This molecule spreads glitter and allows bees to fly over the rainbow. For flying, bees need another molecule that is Sub. Sub is activated and so Sub is a substrate. After eating that molecule bees become very speed and they can fly highly. Pro activate Sub. This means that Sub is catalyzed by Pro."))
my_words <- tolower(c("Pro", "Pra", "Pri", "Pre", "Pru",
"Sab", "Seb", "Sib", "Sob", "Sub"))
sent <- unlist(strsplit(my_text, "\\."))
sent <- sent[grep(pattern = my_words, sent, ignore.case = T)]
使用此代码我收到此警告消息
Warning message:
In grep(pattern = my_words, sent, ignore.case = T) :
argument 'pattern' has length > 1 and only the first element will be used
如何避免这种情况?我想分析我的向量的所有单词。我查看了 stringr 包,但找不到解决方案。
代码无论如何都可以改变,我只是展示了我所做的!
【问题讨论】: