Description Usage Arguments Details Value Examples
rollFun
computes rolling window statistics on vectors or matrices.
1 |
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, |
... |
optional arguments to the corresponding function in caTools
or |
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.
An object having the same attributes as dat.
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))
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.