【发布时间】:2016-11-02 13:27:06
【问题描述】:
我需要使用 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 代码。谢谢!
【问题讨论】:
-
是的,我已经尝试过,但仍然没有得到所需格式的输出。如果你能用代码指导我,那将非常有帮助。
-
您应该在问题中发布您尝试过的代码。
-
也许只是
res <- sapply(phrase, grepl, x = sentence); rownames(res) <- sentence; res?我认为无论您打算对结果做什么,这种格式都会更方便。 -
谢谢卢克A。你的代码给了我一些前进的方向。