Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

r - Regular analysis over irregular time series

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use align.time to put the index of s into the periods you're interested in. Then use period.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.

# align index into 3-hour blocks
a <- align.time(s, n=60*60*3)
# find the number of obs in each block
count <- period.apply(a, endpoints(a, "hours", 3), length)
# create an empty xts object with the desired index
e <- xts(,seq(start(a),end(a),by="3 hours"))
# merge the counts with the empty object and fill with zeros
out <- merge(e,count,fill=0)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...