r - R中时间序列数据的拆分应用聚合

标签 r time-series aggregate plyr xts

我有一些天气预报数据,记录了预测每小时的降雨量。我想将其与观测数据进行比较,其中观测到每 6 小时的降雨量。因此,我需要将预测数据聚合为 6 小时数据。

以下是我的数据概述:

                     DateUtc StationID FcstDay PrecipQuantity_hSum
1        2014-01-01 12:00:00     54745       0                   0
2        2014-01-01 13:00:00     54745       0                   0
3        2014-01-01 14:00:00     54745       0                   0
4        2014-01-01 15:00:00     54745       0                   0
5        2014-01-01 16:00:00     54745       0                   0
6        2014-01-01 17:00:00     54745       0                   0
7        2014-01-01 18:00:00     54745       0                   0
8        2014-01-01 19:00:00     54745       0                   0
9        2014-01-01 20:00:00     54745       0                   0
10       2014-01-01 21:00:00     54745       0                   0
11       2014-01-01 22:00:00     54745       0                   0
12       2014-01-01 23:00:00     54745       0                   0
13       2014-01-02 00:00:00     54745       1                   0
14       2014-01-02 01:00:00     54745       1                   0
15       2014-01-02 02:00:00     54745       1                   0
16       2014-01-02 03:00:00     54745       1                   0
17       2014-01-02 04:00:00     54745       1                   0
18       2014-01-02 05:00:00     54745       1                   0
19       2014-01-02 06:00:00     54745       1                   0
20       2014-01-02 07:00:00     54745       1                   0
...                     <NA>      <NA>     ...                 ...
13802582 2014-11-20 08:00:00     55005       7                   0
13802583 2014-11-20 09:00:00     55005       7                   0
13802584 2014-11-20 10:00:00     55005       7                   0
13802585 2014-11-20 11:00:00     55005       7                   0
13802586 2014-11-20 12:00:00     55005       7                   0

要正确聚合,重要的是按 StationID(气象站)和 FcstDay(计算预测日期与预测日期之间的天数)进行拆分在聚合之前。

我已经使用 xts 包进行聚合,如果我首先手动对数据进行子集化,它会按预期工作,例如

z <- fcst[which(fcst$StationID=="54745" & fcst$FcstDay==1),]
z.xts <- xts(z$PrecipQuantity_hSum, z$DateUtc)
ends <- endpoints(z.xts, "hours", 6)
precip6 <- as.data.frame(period.appl(z.xts, ends, sum))

我需要自动进行子集设置,但我尝试将 xts 函数包装在各种 split-apply 函数中,但总是得到相同的错误:

Error in xts(z$PrecipQuantity_hSum, z$DateUtc) : 
  NROW(x) must match length(order.by)

这是我的代码的最新版本:

df <- data.frame()

  d_ply(
    .data = fcst,
    .variables = c("FcstDay", "StationID"),
    .fun = function(z){
      z.xts <- xts(z$PrecipQuantity_hSum, z$DateUtc)
      ends <- endpoints(z.xts, "hours", 6)
      precip6 <- as.data.frame(period.apply(z.xts, ends, sum))
      precip6$DateUtc <- rownames(precip6)
      rownames(precip6) <- NULL
      df <- rbind.fill(df, precip6)
    })

我还尝试过嵌套的 for 循环。任何人都可以对出了什么问题提供任何指导吗?我已经包含了下面设置的可重现示例的代码。提前致谢。

DateUtc <- rep(seq(from=ISOdatetime(2014,1,1,0,0,0), to=ISOdatetime(2014,12,30,0,0,0), by=(60*60)), times=9)
StationID <- rep(c("50060","50061","50062"), each=3*8713)
FcstDay <- rep(c(1,2,3), each=8713, times=3)
PrecipQuantity_hSum <- rgamma(78417, shape=1, rate=20)
fcst <- data.frame(DateUtc, StationID, FcstDay, PrecipQuantity_hSum)

最佳答案

我认为 David Robinson 遇到的错误是因为您的示例代码使用 PrecipQuantity_6hSum 而不是 PrecipQuantity_hSum。一旦更改,您的 ddply 代码就可以为我工作了。

这对你有用吗?

df<-ddply(
     .data = fcst,
     .variables = c("FcstDay", "StationID"),
     .fun = function(z){
       z.xts <- xts(z$PrecipQuantity_6hSum, z$DateUtc)
       ends <- endpoints(z.xts, "hours", 6)
       precip6 <- as.data.frame(period.apply(z.xts, ends, sum))
       precip6$DateUtc <- rownames(precip6)
       rownames(precip6) <- NULL
       return(precip6)
    })

关于r - R中时间序列数据的拆分应用聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31407967/

相关文章:

Python pandas 按日期列表选择行

sql-server-2008 - SQL Server 2008中的CTE : how to calculate subtotals recursively

SQL Statement JOIN 只返回有关联的元素

r - 计算以观察 == 1 为条件按受试者 ID 聚合的游程长度

r - 将集合的分区枚举为大小相等的子集

r - 按组递增

r - 在R中获取伴随矩阵

performance - 创建一维 NumPy 数组的 NoN 填充元素的滑动窗口

r - 如何使用R中的并行处理来分析大型时间序列数据集

r - 将矩阵转换为R中的栅格