首页 文章

用另一个不规则的时间序列分割时间序列

提问于
浏览
3

我试图用一个独特的不规则时间序列分割几个xts对象 . split.xts 在天,分,秒等上拆分 . 使用断点需要相等长度的向量,这在我尝试拆分数据时会产生错误 .

dd <- c("2014-02-23","2014-03-12", "2014-05-29")
tt <- c("03:15:52", "03:49:17", "04:03:24", "05:30:19", "05:56:49",
        "06:14:04", "09:42:13", "11:57:25", "11:58:02", "12:12:49",
        "15:38:00", "15:44:21", "16:16:04")
dt <- c(outer(dd,tt,paste))
xx <- as.xts(seq_along(dt), as.POSIXct(dt))
spltr <- c("2014-01-13 12:09:32", "2014-02-09 06:23:41",
           "2014-03-01 13:35:12", "2014-05-14 07:12:52")

我试图通过 spltr 拆分 xx 来查找每件中的记录频率 . 我试过 aggregate(xx,by=spltr,length) 但是我收到一个错误,因为 spltrxx 的长度不同 . split.xts 不起作用,因为 spltr 不常规 .

1 回答

  • 3

    首先,将 xx 对象与包含断点的空xts对象合并 .

    xs <- merge(xx, xts(,as.POSIXct(spltr)))
    

    然后,您可以使用 xswhich.i 参数在 xs 中找到 spltr 对象的'endpoints' .

    ep <- c(0,xs[as.POSIXct(spltr),which.i=TRUE])
    

    现在你可以在 xs 对象上使用 period.apply (确保处理任何潜在的 NA ) .

    > period.apply(xs, ep, function(x) nrow(na.omit(x)))
                        xx
    2014-01-13 12:09:32  0
    2014-02-09 06:23:41  0
    2014-03-01 13:35:12 13
    2014-05-14 07:12:52 13
    

相关问题