使用R

时间:2016-11-02 13:27:07

标签: r regex grep gsub

我需要使用R匹配句子中的多个短语并将其保存在旁边的列中。例如:

   sentence <- c("My CPU is working but keyboard is not working", "unable
                  access printer", "unable access printer and keyboard is not working")  
   phrase   <- c("unable access printer", "keyboard is not working")

我希望我的输出采用以下数据帧格式:

      sentence                                             phrase1                  phrase2
  My CPU is working but keyboard is not working         keyboard is not working       NA
  unable access printer                                      NA                       NA
  unable access printer and keyboard is not working     unable access printer        keyboard is not working

请您指导方法或R代码。谢谢!

1 个答案:

答案 0 :(得分:1)

library(quanteda)
library(foreach)
library(data.table)

corp = corpus(sentence)

find.phrases = foreach(i = 1:length(phrase)) %do% {
  kwic(corp, phrase[i])
}

find.df = rbindlist(find.phrases)