【问题标题】:Truncate zoo time series to nearest 10 minutes将动物园时间序列截断到最近的 10 分钟
【发布时间】:2012-01-17 12:07:29
【问题描述】:

我有一个动物园时间序列如下:

>> head(ww)
(11/22/08 11:37:00) (11/22/08 12:07:00) (11/22/08 12:22:00) (11/22/08 12:37:00) 
           0.087114            0.055422            0.055250            0.059483 
(11/22/08 12:52:00) (11/22/08 13:07:00) 
           0.057896            0.061808

如您所见,时间是 11:37、12:07、12:22 等。我想将这些时间更改为最接近的十分钟整数倍 - 例如 XX:10、XX:20。在这种情况下,11:37 将变为 11:40,12:07 将变为 12:10,12:22 将变为 12:20。

我找到了如何将它聚合到最近的分钟:

wholemin <- function(x) trunc(x, units="minutes")
result = aggregate(r, wholemin, head, 1)

但我不能使用 trunc 聚合到秒、分钟、小时等以外的任何值。

我该怎么做呢?

为了便于举例,dput(head(ww) 的输出如下:

structure(c(0.087114, 0.055422, 0.05525, 0.059483, 0.057896, 
0.061808), index = structure(c(14205.4840277778, 14205.5048611111, 
14205.5152777778, 14205.5256944444, 14205.5361111111, 14205.5465277778
), format = structure(c("m/d/y", "h:m:s"), .Names = c("dates", 
"times")), origin = structure(c(1, 1, 1970), .Names = c("month", 
"day", "year")), class = c("chron", "dates", "times")), class = "zoo")

【问题讨论】:

    标签: r time-series zoo


    【解决方案1】:

    试试这个:

    to10 <- function(tt) trunc(tt + as.numeric(times("00:05:00")), units = "00:10:00")
    aggregate(z, to10, mean)
    

    并查看?trunc.times?aggregate.zoo 中的示例。

    【讨论】:

    • 谢谢。这就是我一直在寻找的。您可能想指出 read.zoo() 默认情况下将所有内容都作为因子,因此将数据转换为数字确实很有帮助。
    • 不适合我。例如,这给了我数字数据和索引:str(read.zoo(text = "1 1")) .
    猜你喜欢
    • 2013-05-15
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2012-03-30
    • 2017-10-14
    • 2016-07-02
    相关资源
    最近更新 更多