【问题标题】:Vector operations by time intervals , in R ??, aggregate按时间间隔的向量运算,在 R ?? 中,聚合
【发布时间】:2011-04-01 10:40:54
【问题描述】:

我有一个时间序列(索引类型为 chron 的动物园),我需要在一个新的动物园对象中分别计算 cummax(mydata)-mydata 的每一天。

我试过了:

aggregate(mydata, as.date, cummax)

但是aggregate 只能为每个子集生成一个标量结果,而不是一个向量。我读过也许tapplylapplyplyrcutrollapply 可以做到,但我无法让它们工作。

【问题讨论】:

  • 样本数据在帮助专家(不是我!)方面大有帮助。

标签: r aggregate time-series zoo


【解决方案1】:

zoo 有一个 cummax 方法,所以你应该没有任何问题得到 zoo 结果。也许你让这变得比现在更困难......这就是你想要的吗?

> set.seed(21)
> z <- zoo(runif(10),as.chron(Sys.Date()-10:1))
> merge(z,cummax=cummax(z),diff=cummax(z)-z)
                  z    cummax      diff
08/09/10 0.66754012 0.6675401 0.0000000
08/10/10 0.93521022 0.9352102 0.0000000
08/11/10 0.05818433 0.9352102 0.8770259
08/12/10 0.61861583 0.9352102 0.3165944
08/13/10 0.17491846 0.9352102 0.7602918
08/14/10 0.03767539 0.9352102 0.8975348
08/15/10 0.52531317 0.9352102 0.4098971
08/16/10 0.28218425 0.9352102 0.6530260
08/17/10 0.49904520 0.9352102 0.4361650
08/18/10 0.63382510 0.9352102 0.3013851

由于这很容易,我猜您的时间序列是日内频率。如果是这样的话,代码就更复杂了,但这应该可以解决问题:

> require(xts)  # for the endpoints() function
> set.seed(21)
> z <- zoo(runif(10),as.chron(Sys.Date()-seq(0.5,3,length.out=10)))
> ep <- endpoints(z,"days")
> Z <- lapply(1:(length(ep)-1), function(x) cummax(z[(ep[x]+1):ep[x+1]]))
> Z <- do.call(rbind, Z)
> merge(z,Z,Z-z)
                            z         Z     Z - z
(08/16/10 00:00:00) 0.8493961 0.8493961 0.0000000
(08/16/10 06:40:00) 0.9860037 0.9860037 0.0000000
(08/16/10 13:20:00) 0.1721917 0.9860037 0.8138120
(08/16/10 20:00:00) 0.1018046 0.9860037 0.8841991
(08/17/10 02:40:00) 0.9186834 0.9186834 0.0000000
(08/17/10 09:20:00) 0.9596138 0.9596138 0.0000000
(08/17/10 16:00:00) 0.1844608 0.9596138 0.7751531
(08/17/10 22:40:00) 0.6992523 0.9596138 0.2603615
(08/18/10 05:20:00) 0.2524456 0.2524456 0.0000000
(08/18/10 12:00:00) 0.7861149 0.7861149 0.0000000

【讨论】:

  • 您好,我会研究您的答案,谢谢。同时我必须这样做 tapply(z,as.Date(index(z)), cummax) 但它给了我一个列表,当我尝试将它转换为动物园对象时,使用 unlist 或 unsplit,我得到了奇怪的东西日期。欢呼
  • 这比我的回答简单。您只是缺少do.call 部分:Z &lt;- do.call(rbind, tapply(z, as.Date(index(z)), cummax))
【解决方案2】:

一种粗略的方法:制作一个矩阵,其行或列对应于要进行计算的块,然后使用apply

x <- rnorm(240) # imagine this to be 10 days of hourly data
xm <- matrix(x, ncol=24, byrow=TRUE)
daily.avg <- apply(xm, 1, mean)
plot(x)
lines(12 + seq(1,240,24), daily.avg)

【讨论】:

    【解决方案3】:

    使用 ave 可以在一行中完成:

    > library(zoo)
    > set.seed(123)
    > z <- zoo(rnorm(10), chron(0:9/5))
    >
    > ave(coredata(z), as.Date(time(z)), FUN = cummax) - z
    (01/01/70 00:00:00) (01/01/70 04:48:00) (01/01/70 09:36:00) (01/01/70 14:24:00) (01/01/70 19:12:00) (01/02/70 00:00:00) (01/02/70 04:48:00) 
               0.000000            0.000000            0.000000            1.488200            1.429421            0.000000            1.254149 
    (01/02/70 09:36:00) (01/02/70 14:24:00) (01/02/70 19:12:00) 
               2.980126            2.401918            2.160727 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 2014-03-09
      相关资源
      最近更新 更多