rollapply: Apply a function along a vector or a list

Description Usage Arguments Examples

Description

Apply a function along a vector or a list

Usage

1
2
3
rollapply(obj, fun, w = 5, ..., wty = c("moving", "fixed"),
  simplify = TRUE, aty = c("single", "multiple"),
  nas = pmatch(match.arg(aty), "single", 0))

Arguments

obj

A list or an atomic vector.

fun

A function to be applied.

w

Width of the window.

...

Optional arguments to fun.

wty

Window type: to choose between c('moving', 'fixed')).

simplify

Logical or character string. Should the result be simplified to a vector, matrix or higher dimensional array if possible?

aty

Argument type: How the elements of obj should be given to fun. 'single' if they should be regouped in one argument. 'multiple' if they should be considered as separated arguments. See the last example.

nas

Should NAs be added to the result to match the length of the input.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
x <- seq(1, 3, length = 150) + rt(150, df = 2) / 3
plot(x)
av  <- rollapply(x, mean, 15)
lines(av, lwd = 2)
err <- rollapply(x, sd, 15)
lines(av + err, lwd = 2, lty = 2) ; lines(av - err, lwd = 2, lty = 2)

# 'wty' argument
lines(rollapply(x, mean, wty = 'f'), type = 's', col = 'red')

# 'aty' argument
x <- 2^(0:5)
out <- rollapply(x, seq, 2, aty = 'm')
names(out) <- rollapply(x, paste, 2, sep = ':', aty = 'm')
out

SESman/rbl documentation built on May 9, 2019, 11:10 a.m.