【发布时间】:2016-03-31 03:33:16
【问题描述】:
我正在尝试使用 grepl 检测术语,但误报过多。我希望可能有一种方法可以要求列表中任何术语的两次成功匹配(我对我的一段数据进行手动编码,并试图让自动化至少大致对应于此,但我有大约 5 个是我手动编码的两倍)。我没有看到 grepl 接受任何需要多个匹配项才能触发 TRUE 的参数。有没有办法要求两个匹配来触发 TRUE 发现?还是我应该使用其他一些功能?
GenericColumn <- cbind(grepl(Genericpattern, Statement$Statement.Text, ignore.case = TRUE))
编辑:
这是一个更具体的例子:
Examplepattern <- 'apple|orange'
ExampleColumn <- cbind(grepl(Examplepattern, Rexample$Statement.Text, ignore.case = TRUE))
现在,所有这些都将通过 grepl 触发。我只希望有两个引用的项目触发 true。
示例数据:
Rexample <- structure(list(Statement.Text = structure(c(2L, 1L, 3L, 5L, 4L
), .Label = c("This apple is a test about an apple.", "This is a test about apples.",
"This orange is a test about apples.", "This orange is a test about oranges.",
"This orange is a test."), class = "factor")), .Names = "Statement.Text", row.names = c(NA,
5L), class = "data.frame")
所需的输出:真、假、真、真、假
【问题讨论】:
-
我不确定最好的具体示例是什么 - 实际代码中没有大量信息。我有一个模式:
codeGenericpattern -
请参阅how to write a great reproducible R example,以便我们为您提供帮助。确实,您可能应该使用 HTML 解析器进行解析,而不是正则表达式。
rvest::html_nodes应该可以解决问题,搭配 CSS 或 XPath 选择器。 -
我已经将数据保存在 csv 中,所以它已经从网站上移到了一个可操作的文件中——我现在只是想匹配术语两次而不是一次。我将按照该链接查看是否可以提出一个可重现的示例,尽管这与其说是错误问题,不如说是功能问题。
-
@alistaire 我在编辑的初始帖子中添加了一个具体示例(我认为)。