I have time-series data of five days, formatted as xts
object. 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. Here, I am facing two problems:
How should I run for loop over xts
time indices. I mean to say, can I traverse using minutes
of xts
object?
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. Is there any defined function to group time-series observations dynamically?
In all these operations, I don't want to lose xts
index.
You could split
your data by HH:MM:SS, then loop over the resulting list.
# 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.