Learn R Programming

lambda.tools (version 1.0.9)

foldrange: Successively apply a function to a rolling range of a sequence

Description

Apply a function to a rolling range of a sequence and the accumulated value of the previous function applications

Arguments

x
Any indexable data structure
window
The length of the sub-sequence passed to fn
fn
The function applied to the rolling range
acc
An object that stores the intermediate accumulated result

Value

The accumulated result

Usage

foldrange(x, window, fn, acc, idx) foldrange(x, window, fn, acc, 0) foldrange(x, window, fn, acc=0, idx=length(x)-window+1) When ! is.null(dim(x)) foldrange(x, window, fn, acc=0)

Details

This function is the fold counterpart of maprange. It's primarily here for completeness purposes, as the utility of this function is still to be determined.

See Also

map fold foldblock

Examples

Run this code
## Not run: 
# # Mean of rolling means
# z <- sapply(1:500, 
#   function(x) foldrange(rnorm(50), 10, function(a,b) mean(a) + b) / 41)
# ## End(Not run)

Run the code above in your browser using DataLab