识别 NA 部分的开始和结束项

发布于 2024-12-12 22:47:17 字数 433 浏览 0 评论 0原文

假设我在 R 中有一些数据,如下所示:

c(0.11, NA, NA, NA, 2.76, 3.65, NA, NA, NA, NA, 1.56)

How might I effective extract the start and end term of every "block" of NA value?如果结果是一个数据框,我希望它看起来像这样:

  first.na last.na
1        2       4
2        7      10

我正在尝试训练自己以避免 for 循环,因为我将在非常大的数据集上执行此类操作(按 1e9 项的顺序),并且 na.omit 不太有帮助。

Let's say I have some data in R that looks like this:

c(0.11, NA, NA, NA, 2.76, 3.65, NA, NA, NA, NA, 1.56)

How might I efficiently extract the start and end terms of each "block" of NA values? If the result were a data frame, I would want it to look something like this:

  first.na last.na
1        2       4
2        7      10

I'm trying to train myself to avoid for loops since I'll be doing this type of operation on very large datasets (on the order of 1e9 terms), and na.omit isn't quite helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

囚我心虐我身 2024-12-19 22:47:17

也许有一个函数可以完成这项工作,但你可以这样做:

> z <- c(0.11, NA, NA, NA, 2.76, 3.65, NA, NA, NA, NA, 6)

> z2 <- diff(is.na(c(0, z, 0)))
> data.frame(first.na = which(z2 == 1), last.na = which(z2 == -1)-1)
  first.na last.na
1        2       4
2        7      10

Maybe there is a function to do that work, but you can do by:

> z <- c(0.11, NA, NA, NA, 2.76, 3.65, NA, NA, NA, NA, 6)

> z2 <- diff(is.na(c(0, z, 0)))
> data.frame(first.na = which(z2 == 1), last.na = which(z2 == -1)-1)
  first.na last.na
1        2       4
2        7      10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文