roll: Rolling operations

Description Usage Arguments Value Examples

View source: R/081-roll.R

Description

Apply rolling operations over a moving window for size n and increment step.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
roll_idx(
  x,
  n = 1,
  step = 1,
  align = c("left", "center", "right"),
  complete = TRUE
)

roll(
  x,
  fn,
  n = 1L,
  step = 1L,
  fill = NA,
  align = c("left", "center", "right"),
  ...
)

roll_data(x, n = 1L, step = 1L, align = c("left", "center", "right"))

Arguments

x

[univariate vector]

Univariate vector, numeric or ts object with only one dimension.

n

[positive integer(1):1L]

Window size.

step

[positive integer(1):1L]

Rolling window step.

align

[character(1): "left"]

Specifying whether the index of the result should be left- or right-aligned or centered (default) compared to the rolling window of observations.

fn

[function]

fill

[numeric or function: NA]

Numeric value(s) or function used to fill observations.

...

Further arguments passed to the function fn.

Value

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# x is odd

roll_idx(1:9, 2, 1, align = "left")
roll_idx(1:9, 2, 1, align = "center")
roll_idx(1:9, 2, 1, align = "right") # reduces

# This works
roll_idx(1:9, 3, 1, align = "left")
roll_idx(1:9, 3, 1, align = "center")
roll_idx(1:9, 3, 1, align = "right")

roll_idx(1:9, 2, 2, align = "left")
roll_idx(1:9, 2, 2, align = "center")
roll_idx(1:9, 2, 2, align = "right")

roll_idx(1:9, 3, 2, align = "left")
roll_idx(1:9, 3, 2, align = "center")
roll_idx(1:9, 3, 2, align = "right") # reduces

# x is even

roll_idx(1:8, 2, 2, align = "left")
roll_idx(1:8, 2, 2, align = "center")
roll_idx(1:8, 2, 2, align = "right") # reduces

roll_idx(1:8, 2, 1, align = "left")
roll_idx(1:8, 2, 1, align = "center")
roll_idx(1:8, 2, 1, align = "right")

roll_idx(1:8, 3, 1, align = "left")
roll_idx(1:8, 3, 1, align = "center")
roll_idx(1:8, 3, 1, align = "right") # reduces

roll_idx(1:8, 3, 1, align = "left")
roll_idx(1:8, 3, 1, align = "center")
roll_idx(1:8, 3, 1, align = "right") # reduces


x <- seq(10, 1, -1)

roll_data(x, 2, align = "left")
roll_data(x, 2, align = "right")

roll(x, max, 3)
roll(x, max, 3, align = "right")


x <- 1:6
roll_data(x, 2)
roll(x, mean, 2)

roll_data(x, 2, 2)
roll(x, mean, 2, 2)

kvasilopoulos/transx documentation built on Jan. 26, 2021, 6:14 p.m.