在 R 中动态分组时间序列观察

Group time-series observations dynamically in R

我有五天的时间序列数据,格式为 xts 对象。数据生成为:

library(xts)
Sys.setenv(TZ="Asia/Kolkata")
seq <- timeBasedSeq('2015-06-01/2015-06-05 23')
z <- xts(1:length(seq),seq)

现在,我想将具有相似时间戳(仅 H:M:S)的数据分组,在 for 循环中动态地分组,然后对每个组执行所需的操作。在这里,我面临两个问题:

  1. 我应该如何 运行 循环遍历 xts 时间索引。我的意思是,我可以使用 xts 对象的 minutes 遍历吗?

  2. 我应该如何将具有相似时间戳的观测值分组并执行所需的操作。例如,找到 11 A.M 处的所有观测值。所有 5 天并计算回归系数。是否有任何定义的函数来动态分组时间序列观察?

在所有这些操作中,我不想丢失 xts 索引。

您可以 split 您的数据 HH:MM:SS,然后遍历结果列表。

# 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)