简体   繁体   中英

R zoo series sliding window calculation

Given I have a zoo dataset, I'd like to perform a sliding operation against it with the result being another zoo dataset.

> x
                      Y
(09/24/09 08:00:13)   3.1
(09/24/09 08:05:13)   4.2
(09/24/09 08:10:13)   4.5
(09/24/09 08:15:13)   9.4
(09/24/09 08:20:13)   9.8
(09/24/09 08:25:13)   7.7
(09/24/09 08:30:13)  13.3
(09/24/09 08:35:13)   6.5
(09/24/09 08:40:13)  14.7
(09/24/09 08:45:13)  23.5
(09/24/09 08:50:13)  20.9
(09/24/09 08:55:13)   8.5

My goal is to produce a "smooth" average by iterating through each time interval and obtaining the mean for the set of Y points that are +/- 15 minutes of the current point.

I have a bucketing method of averaging working, but it reduces the resolution of the data. I haven't worked out how to make relative subsets out of zoo with artibrary math, window should help but accessing the index is being difficult.

Thanks.

Use rollmean . Here's an example:

x.Date <- as.Date(paste(2004, rep(1:4, 4:1), sample(1:28, 10), sep = "-"))
x <- zoo(rnorm(12), x.Date)
rollmean(x, 3)

In you case, you will want to lag your data after taking the mean since you the mean around a point in time.

Lastly, you should know about the rollapply function in zoo which can do a rolling operation on any function (including one that you define).

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM