grep 在 data.frame 中的任何单元格上

发布于 2024-12-28 23:58:33 字数 621 浏览 1 评论 0原文

一个简单的“有没有更好的方法”问题。我想查找 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

最后一行执行了我想要的操作,但它看起来很丑,我想知道我是否缺少一些更简单的东西。

背景:dt=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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夢归不見 2025-01-04 23:58:33

这又如何呢?

d=data.frame(V1=c("xxx","yyy","zzz"), V2=c(NA,"ewruinwe",NA))
apply(d, c(1,2), function(x) grepl("ruin", x))
        V1    V2
[1,] FALSE FALSE
[2,] FALSE  TRUE
[3,] FALSE FALSE

正如评论中所述,“2”与“c(1,2)”的作用相同。然后给出单个布尔值:

any(apply(d, 2, function(x) grepl("ruin", x)))
[1] TRUE

What about this?

d=data.frame(V1=c("xxx","yyy","zzz"), V2=c(NA,"ewruinwe",NA))
apply(d, c(1,2), function(x) grepl("ruin", x))
        V1    V2
[1,] FALSE FALSE
[2,] FALSE  TRUE
[3,] FALSE FALSE

As noted in the comments "2" does the same as "c(1,2)". Then to give a single boolean value:

any(apply(d, 2, function(x) grepl("ruin", x)))
[1] TRUE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文