对不规则时间序列的定期分析
我有一个不规则的时间序列(R
中的 xts
),我想对其应用一些时间窗口。例如,给定如下所示的时间序列,我想计算从 2009-09-22 00:00:00
开始的每个离散 3 小时窗口中有多少个观测值:
library(lubridate)
s <- xts(c("OK", "Fail", "Service", "OK", "Service", "OK"),
ymd_hms(c("2009-09-22 07:43:30", "2009-10-01 03:50:30",
"2009-10-01 08:45:00", "2009-10-01 09:48:15",
"2009-11-11 10:30:30", "2009-11-11 11:12:45")))
我显然不能使用 period.apply() 或 split() 来执行此操作,因为它们会忽略没有观察的周期,而且我不能给它一个开始时间。
如果我一次汇总 3 天,我对简单计数问题的期望输出(当然,我的实际任务在每个部分都更加复杂!)将是这样的:
2009-09-22 1
2009-09-25 0
2009-09-28 0
2009-10-01 3
2009-10-04 0
2009-10-07 0
2009-10-10 0
2009-10-13 0
2009-10-16 0
2009-10-19 0
2009-10-22 0
2009-10-25 0
2009-10-28 0
2009-10-31 0
2009-11-03 0
2009-11-06 0
2009-11-09 2
感谢您的指导。
I have an irregular time series (xts
in R
) that I want to apply some time-windowing to. For example, given a time series like the following, I want to compute things like how many observations there are in each discrete 3-hour window, starting from 2009-09-22 00:00:00
:
library(lubridate)
s <- xts(c("OK", "Fail", "Service", "OK", "Service", "OK"),
ymd_hms(c("2009-09-22 07:43:30", "2009-10-01 03:50:30",
"2009-10-01 08:45:00", "2009-10-01 09:48:15",
"2009-11-11 10:30:30", "2009-11-11 11:12:45")))
I apparently can't use period.apply()
or split()
to do it, because those will omit periods with no observations, and I can't give it a starting time.
My desired output for the simple counting problem (though, of course, my real tasks are more complicated with each segment!) would be something like this if I aggregated 3 days at a time:
2009-09-22 1
2009-09-25 0
2009-09-28 0
2009-10-01 3
2009-10-04 0
2009-10-07 0
2009-10-10 0
2009-10-13 0
2009-10-16 0
2009-10-19 0
2009-10-22 0
2009-10-25 0
2009-10-28 0
2009-10-31 0
2009-11-03 0
2009-11-06 0
2009-11-09 2
Thanks for any guidance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
align.time
将s
的索引放入您感兴趣的周期中。然后使用period.apply
查找 的长度每个 3 小时窗口。然后将其与具有所需所有索引值的空 xts 对象合并。Use
align.time
to put the index ofs
into the periods you're interested in. Then useperiod.apply
to find the length of each 3-hour window. Then merge it with an empty xts object that has all the index values you want.