grep 在 data.frame 中的任何单元格上
一个简单的“有没有更好的方法”问题。我想查找 data.frame 中的任何单元格是否包含我正在寻找的子字符串:
d=data.frame(V1=c("xxx","yyy","zzz"), V2=c(NA,"ewruinwe",NA))
grepl("ruin",d[2,2]) #TRUE
grepl("ruin",d) #FALSE FALSE
any(grepl("ruin",as.character(as.matrix(d)))) #TRUE
最后一行执行了我想要的操作,但它看起来很丑,我想知道我是否缺少一些更简单的东西。
背景:d
是t=readHTMLTable(url)
(XML包)中的元素之一。我正在使用 d[2,2] 方法来检查错误消息,并且刚刚发现该网站有时会在 HTML 表格中添加另一行,将我正在查找的错误消息推送到另一个单元格。
更新:所以,似乎有两个选择(感谢mathematica.coffee和Roman Luštrik):
any(grepl("ruin",as.matrix(d)))
any(apply(d, 2, function(x) grepl("ruin", x)))
A simple "is there a better way" question. I want to find if any cell in a data.frame contains the sub-string I'm looking for:
d=data.frame(V1=c("xxx","yyy","zzz"), V2=c(NA,"ewruinwe",NA))
grepl("ruin",d[2,2]) #TRUE
grepl("ruin",d) #FALSE FALSE
any(grepl("ruin",as.character(as.matrix(d)))) #TRUE
The last line does what I want, but it looks so ugly I'm wondering if I'm missing something simpler.
Background: d
is one of the elements in t=readHTMLTable(url)
(XML package). I was doing the d[2,2] approach, to check for an error message, and just discovered the website sometimes add another row to the HTML table, pushing the error message I was looking for to another cell.
UPDATE: so, it seems the two choices (thanks to mathematical.coffee and Roman Luštrik) are:
any(grepl("ruin",as.matrix(d)))
any(apply(d, 2, function(x) grepl("ruin", x)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这又如何呢?
正如评论中所述,“2”与“c(1,2)”的作用相同。然后给出单个布尔值:
What about this?
As noted in the comments "2" does the same as "c(1,2)". Then to give a single boolean value: