[英]Group time-series observations dynamically in R
I have time-series data of five days, formatted as xts
object.我有五天的时间序列数据,格式为
xts
对象。 The data is generated as:数据生成如下:
library(xts)
Sys.setenv(TZ="Asia/Kolkata")
seq <- timeBasedSeq('2015-06-01/2015-06-05 23')
z <- xts(1:length(seq),seq)
Now, I want to group the data with similar timestamps (only H:M:S ), dynamically in a for loop and then perform required operation on each group.现在,我想对具有相似时间戳(仅H:M:S )的数据进行分组,动态地在 for 循环中,然后对每个组执行所需的操作。 Here, I am facing two problems:
在这里,我面临两个问题:
How should I run for loop over xts
time indices.我应该如何在
xts
时间索引上运行 for 循环。 I mean to say, can I traverse using minutes
of xts
object?我的意思是说,我可以使用
minutes
的xts
对象遍历吗?
How should I group the observations with similar time stamps and perform the required operation.我应该如何将具有相似时间戳的观察结果分组并执行所需的操作。 For example, find all the observations at 11 AM of all 5 days and compute regression coefficients.
例如,查找所有 5 天上午 11 点的所有观测值并计算回归系数。 Is there any defined function to group time-series observations dynamically?
是否有任何定义的函数来动态地对时间序列观察进行分组?
In all these operations, I don't want to lose xts
index.在所有这些操作中,我不想丢失
xts
索引。
You could split
your data by HH:MM:SS, then loop over the resulting list.您可以按 HH:MM:SS
split
数据,然后遍历结果列表。
# convert to factor because split.xts will pass f to endpoints() if f is character
# (even if it's more than one element). split.zoo is called if f is factor.
y <- split(z, factor(format(index(z), "%H%M%S")))
# loop over each time group
l <- lapply(y, FUN)
# combine results
x <- do.call(rbind, l)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.