rollFun: Compute rolling (a.k.a. moving) window statistics

Description Usage Arguments Details Value Examples

View source: R/matstat.R

Description

rollFun computes rolling window statistics on vectors or matrices.

Usage

1
rollFun(dat, width, FUN, force_rollapply = FALSE, ...)

Arguments

dat

a numeric vector, matrix or data.frame. In the latter cases rolling statistics are computed column-wise.

width

width of moving window; can be an integer value or vector.

FUN

the function to be applied to compute moving window statistics. See details.

force_rollapply

logical variable; if yes, zoo::rollapply is called (default = FALSE).

...

optional arguments to the corresponding function in caTools or zoo::rollapply

Details

If FUN is one of min, max, mean, sd, mad, quantile (OR "min", "max", "mean", etc.) rollFun calls the corresponding function from the caTools package (e.g. caTools::runmin). Otherwise, or if force_rollapply is TRUE, zoo::rollapply is called.

Value

An object having the same attributes as dat.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# either caTools or zoo must be installed before using this function;
# here follows a timing comparison for caTools and zoo, so we need both
if (require(caTools) && require(zoo)) {
    # create a matrix
    x <- matrix_(rnorm(2e4), 1e2, 2e2)

    # compute rolling mean for each columns, set the width of the 
    # sliding window to 5
    system.time(roll_mean_catools <- rollFun(x, 5, mean))
    system.time(roll_mean_zoo <- rollFun(x, 5, mean, force_rollapply = TRUE))
    
    # caTools is much faster for the standard statistics, and the results
    # are the same
    stopifnot(all.equal(roll_mean_catools, roll_mean_zoo))
}

tdeenes/eegR documentation built on April 19, 2021, 4:17 p.m.