【发布时间】:2017-11-21 09:03:07
【问题描述】:
我正在寻找一种使用另一个字符向量扫描字符向量的方法。我已经在这方面投入了很多时间,但似乎无法做到正确,分别。我找不到可以做我打算做的功能。但我确信有一个简单的方法可以解决这个问题
假设我有以下向量:
c <- c("bread", "milk", "oven", "salt")
另一方面,我有一个包含句子的向量。
text <- c("The BREAD is in the oven. Wonderful!!",
"We don't only need Milk to bake a yummy bread, but a pinch of salt as
well.", "Oven, oven, oven, why not just eat it raw.")
现在我想使用我的 c 向量的内容来扫描文本块。输出应该是这样的:
text bread milk oven salt
1 The BREAD is in the oven. Wonderful!! 1 0 1 0
2 We don't only need Milk... as well." 0 1 0 1
3 Oven, oven, oven, why not just eat it raw. 0 0 3 0
我想做的另一件事是搜索组合,而不仅仅是单个单词。
c <- c("need milk", "oven oven", "eat it")
得到相同的输出:
text need milk oven oven eat it
1 The BREAD is in the oven. Wonderful!! 0 0 0
2 We don't only need Milk... as well." 1 0 1
3 Oven, oven, oven, why not just eat it raw. 0 2 1
如果有人可以帮助我,那就太好了! :) 非常感谢!
【问题讨论】:
-
试试
data.frame(text, +(sapply(c, grepl, tolower(text)))) -
太棒了。你能帮助了解
+在这里做什么吗? -
@amrrs
grepl返回一个逻辑矩阵。通过添加+或*1,它强制转换为二进制 -
您的示例“文本”和显示的输出“文本”不匹配。第二部分不清楚。在您的文字中,它是
Oven, oven, oven,您正在尝试匹配“烤箱” -
我不确定 OP 是否在那里提到了二元组 - 出现了 2 次烤箱烤箱。
标签: r vector find text-mining