【问题标题】:How can I manipulate my data to compute a mean in R with multiple NA instances?如何操作我的数据以计算具有多个 NA 实例的 R 中的平均值?
【发布时间】:2020-04-08 16:54:24
【问题描述】:

我正在使用 R studio,并且有一个非常大的数据集,其中包含 4 个变量。我正在尝试计算 O2 柱每天 06:30:00 到 19:00:00 之间小时的平均值。数据集的示例如下所示:

RBWQ <- read.csv(".../R/WY06 to WY19.csv", skip = 2, header = TRUE, sep = ",", na.strings=c("","NA"))
RBWQ <- setNames(RBWQ, c("datetime","NDCQ","O2","SpCond","PaseoQ"))
View(RBWQ)

    datetime              NDCQ O2   SpCond PaseoQ
1   2005-10-01 00:00:00   NA   NA   NA     827
2   2005-10-01 00:15:00   NA   NA   NA     857
3   2005-10-01 00:30:00   NA   NA   NA     857
4   2005-10-01 00:45:00   NA   NA   NA     807
5   2005-10-01 01:00:00   NA   10   300    797
6   2005-10-01 01:15:00   NA   10.3 319    817
7   2005-10-01 01:30:00   NA   10   401    797
8   2005-10-01 01:45:00   NA   10.2 398    788
9   2005-10-01 02:00:00   NA   NA   NA     788
10  2005-10-01 02:15:00   NA   NA   NA     807

每小时有 15 分钟的间隔,您可以看到此数据集中有很多 NA。如何操作此数据集以确定每天特定时间段内的平均值,并让它在表格中生成报告,其中包含日期和 R 中的平均值?

dput(head(RBWQ))

    structure(list(datetime = structure(list(sec = c(0, 0, 0, 0, 
0, 0), min = c(0L, 15L, 30L, 45L, 0L, 15L), hour = c(0L, 0L, 
0L, 0L, 1L, 1L), mday = c(1L, 1L, 1L, 1L, 1L, 1L), mon = c(9L, 
9L, 9L, 9L, 9L, 9L), year = c(105L, 105L, 105L, 105L, 105L, 105L
), wday = c(6L, 6L, 6L, 6L, 6L, 6L), yday = c(273L, 273L, 273L, 
273L, 273L, 273L), isdst = c(1L, 1L, 1L, 1L, 1L, 1L), zone = c("MDT", 
"MDT", "MDT", "MDT", "MDT", "MDT"), gmtoff = c(NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_)), class = c("POSIXlt", 
"POSIXt")), NDCQ = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), O2 = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), SpCond = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_), PaseoQ = c(827L, 857L, 857L, 807L, 
797L, 817L)), row.names = c(NA, 6L), class = "data.frame")

由于各种原因(例如被埋没),仪器读数不正确时,某些日子可能会出现一整天的 NA。

dput(head(RBWQ, 96))
    structure(list(datetime = structure(list(sec = c(0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 15L, 30L, 45L, 0L, 15L, 
    30L, 45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 
    45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L, 
    0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L, 0L, 
    15L, 30L, 45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L, 0L, 15L, 
    30L, 45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 
    45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L, 
    0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L, 0L, 15L, 30L, 45L), hour = c(0L, 
    0L, 0L, 0L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 
    4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 
    8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 
    11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 
    15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 18L, 
    18L, 18L, 18L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 21L, 21L, 
    21L, 21L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L), mday = c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
        mon = c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
        9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
        9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
        9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
        9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
        9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
        9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L), year = c(105L, 105L, 105L, 
        105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
        105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
        105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
        105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
        105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
        105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
        105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
        105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
        105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
        105L, 105L, 105L), wday = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
        6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), yday = c(273L, 
        273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 
        273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 
        273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 
        273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 
        273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 
        273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 
        273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 
        273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 
        273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 273L, 
        273L, 273L, 273L, 273L, 273L), isdst = c(1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L), zone = c("MDT", "MDT", "MDT", "MDT", "MDT", "MDT", 
        "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", 
        "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", 
        "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", 
        "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", 
        "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", 
        "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", 
        "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", 
        "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", 
        "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", 
        "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT", "MDT"
        ), gmtoff = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
        NA_integer_, NA_integer_)), class = c("POSIXlt", "POSIXt"
    )), NDCQ = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
    ), O2 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), 
        SpCond = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
        NA_real_), PaseoQ = c(827L, 857L, 857L, 807L, 797L, 817L, 
        797L, 788L, 788L, 807L, 788L, 797L, 807L, 797L, 778L, 797L, 
        807L, 797L, 750L, 741L, 750L, 714L, 714L, 696L, 696L, 687L, 
        687L, 678L, 678L, 661L, 669L, 661L, 678L, 678L, 696L, 661L, 
        644L, 635L, 661L, 644L, 644L, 635L, 635L, 627L, 635L, 652L, 
        652L, 652L, 644L, 635L, 644L, 652L, 661L, 652L, 661L, 635L, 
        661L, 644L, 652L, 635L, 644L, 619L, 619L, 619L, 635L, 635L, 
        635L, 652L, 635L, 652L, 652L, 661L, 652L, 652L, 652L, 652L, 
        661L, 652L, 644L, 627L, 619L, 635L, 619L, 619L, 603L, 611L, 
        603L, 603L, 619L, 635L, 635L, 635L, 627L, 619L, 635L, 619L
        )), row.names = c(NA, 96L), class = "data.frame")

【问题讨论】:

  • 感谢您的欢迎!我很感激它以及您可以提供的任何帮助。有没有添加 96 行数据的好方法,以便我可以上传一整天?我应该在我的问题中复制粘贴吗?
  • 您可以按照@Ben 的建议从dput 获取代码后将其编辑到您的问题中。或者,您可以将其上传到 GitHub 或 PasteBin 并编辑问题的链接。
  • 从这一行开始 "NA_real_, NA_real_, NA_real_ ), O2 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_," 代表 O2
  • 是的,但每个值都是NA_real_。我会用模拟数据回答你的问题,希望对你有用。
  • 嗨伊恩,这是来自 PasteBin 的链接pastebin.com/Zeh4u5Cq

标签: r mean


【解决方案1】:

对于时间序列数据,请考虑xts 包。根据您的数据创建一个时间序列(第一列中包含您的日期时间)。然后根据时间范围进行子集化,并使用colMeans 计算每日均值(删除NA)。如果这是您的想法,请告诉我。

library(xts)

RBWQ_xts <- xts(RBWQ[, -1], RBWQ[, 1])

apply.daily(RBWQ_xts["T06:30:00/T19:00:00"], colMeans, na.rm = TRUE)

【讨论】:

  • 非常感谢您的帮助。我收到以下错误: xts(RBWQ[, -1], RBWQ[, 1]) 中的错误:'order.by' 不能包含 'NA'、'NaN' 或 'Inf' 所以我不能确定这是否是我一直在寻找的。你知道我是如何克服这个错误的吗?
  • @Eddie 您可能在数据中将NA 作为日期时间?如果是这样,那么xts 将给出一个错误。与其尝试对所有数据执行此操作,不如尝试在您拥有数据的几天内进行子集化,看看这是否能满足您的需求?
  • 好的,我会这样做,看看是否可以运行测试以确定日期时间是否存在 NA。不应该……
【解决方案2】:

由于行数太多,我推荐data.table 方法。

首先,我们分配新列datetime,以便我们可以在下一步轻松过滤它们。我们可以使用优化的data.table 函数as.IDateas.ITime 以便快速进行计算。

接下来,我们会过滤您感兴趣的时间。

我们使用基本 R 的 mean 函数计算均值,并使用参数 na.rm = TRUE

最后,我们将by = date 分组,以便计算并返回每一天的平均值。

library(data.table)
setDT(RBWQ)[,`:=`(date = as.IDate(datetime),time = as.ITime(datetime))][
  time > as.ITime("06:30:00") & time < as.ITime("19:00:00"),
  .(meanO2 = mean(O2,na.rm=TRUE)),
  by = date]
#         date    meanO2
#1: 2005-10-01 10.210526
#2: 2005-10-02  9.910526

我怀疑大约 450000 行的计算可以在几秒钟内完成。

【讨论】:

  • 我同意data.table 对您拥有的数据量很有意义。
  • 我收到以下错误:错误在 min * 60L : non-numeric argument to binary operator
  • 尝试setDT(RBWQ)[,datetime := as.POSIXct(datetime)],然后再次尝试答案中的代码。
  • 我收到了同样的错误。我真的很感谢你们两个抽出时间来帮助我。
猜你喜欢
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2021-04-16
  • 1970-01-01
  • 2021-07-03
  • 2020-12-16
相关资源
最近更新 更多