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

View source: R/row.r

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

Details

Originally written for the row.r package by Craig Varrichio. Included here because the rowr package was discontinued. I use these functions in my packages

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


ccpluncw/ccpl_R_chutils documentation built on Feb. 28, 2024, 1:17 a.m.