将一个时间序列拆分为另一个不规则的时间序列 [英] split a time series by another irregular time series

查看:48
本文介绍了将一个时间序列拆分为另一个不规则的时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个独特的不规则时间序列拆分多个 xts 对象.split.xts 按天、分钟、秒等拆分.使用断点需要等长的向量,当我尝试拆分数据时会产生错误.

I am trying to split several xts objects with one unique irregular time series. split.xts splits on days, minutes, seconds, etc. Using breakpoints requires vectors of equal length, which produces an error when I try to split my data.

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 不正常.

I am trying to split xx by spltr to find the frequency of records in each piece. I tried aggregate(xx,by=spltr,length) but I get an error because spltr is not the same length as xx. split.xts doesn't work because spltr is not regular.

推荐答案

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

First, merge your xx object with an empty xts object containing your breakpoints.

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

然后您可以使用 which.i 参数在 xs 中找到 spltr 对象的端点".xts.

Then you can find the 'endpoints' of your spltr object in xs by using the which.i argument to [.xts.

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

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

Now you can use period.apply on the xs object (making sure to deal with any potential 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

这篇关于将一个时间序列拆分为另一个不规则的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆