[英]split a time series by another irregular time series
I am trying to split several xts objects with one unique irregular time series. 我试图用一个独特的不规则时间序列分割几个xts对象。 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. 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")
I am trying to split xx
by spltr
to find the frequency of records in each piece. 我试图通过spltr
分割xx
以找到每个片段中的记录频率。 I tried aggregate(xx,by=spltr,length)
but I get an error because spltr
is not the same length as xx
. 我尝试了aggregate(xx,by=spltr,length)
但是我得到一个错误,因为spltr
与xx
长度不同。 split.xts
doesn't work because spltr
is not regular. split.xts
不起作用,因为spltr
不是常规的。
First, merge your xx
object with an empty xts object containing your breakpoints. 首先,将xx
对象与包含断点的空xts对象合并。
xs <- merge(xx, xts(,as.POSIXct(spltr)))
Then you can find the 'endpoints' of your spltr
object in xs
by using the which.i
argument to [.xts
. 然后,您可以使用[.xts
的which.i
参数]在xs
找到spltr
对象的“端点”。
ep <- c(0,xs[as.POSIXct(spltr),which.i=TRUE])
Now you can use period.apply
on the xs
object (making sure to deal with any potential NA
). 现在你可以在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.