Last data update: 2014.03.03

R: Applies a function over a rolling window on any data object.
rollApplyR Documentation

Applies a function over a rolling window on any data object.

Description

Simple generalized alternative to rollapply in package zoo with the advantage that it works on any type of data structure (vector, list, matrix, etc) instead of requiring a zoo object.

Usage

rollApply(data, fun, window = len(data), minimum = 1, align = "left", ...)

Arguments

data

any R object

fun

the function to evaluate

window

window width defining the size of the subset available to the fun at any given point

minimum

minimum width of the window. Will not return results if the window is truncated below this value at the end of the data set

align

whether to align the window right or left

...

additional arguments to pass to fun

Examples

rollApply(1:100,sum,minimum=2,window=2)
rollApply(c(1,2,3),sum)
##6 5 3
rollApply(c(1,2,3,4,5,6,7,8,9),sum)
##45 44 42 39 35 30 24 17  9
rollApply(c(1,2,3,4,5,6,7,8,9),sum,window=2)
##3  5  7  9 11 13 15 17  9
rollApply(list(1,2,3,4,5,6,7,8,9),function(x) sum(unlist(x)),window=2,minimum=2)
##3  5  7  9 11 13 15 17
cbind(women,Rolling3=rollApply(women,fun=function(x) mean(x$weight),window=3,align='right'))

Results