roll_sum: Rolling Sums

Description Usage Arguments Value Examples

View source: R/roll.R

Description

A function for computing the rolling and expanding sums of time-series data.

Usage

1
2
roll_sum(x, width, weights = rep(1, width), min_obs = width,
  complete_obs = FALSE, na_restore = FALSE, online = TRUE)

Arguments

x

vector or matrix. Rows are observations and columns are variables.

width

integer. Window size.

weights

vector. Weights for each observation within a window.

min_obs

integer. Minimum number of observations required to have a value within a window, otherwise result is NA.

complete_obs

logical. If TRUE then rows containing any missing values are removed, if FALSE then each value is used.

na_restore

logical. Should missing values be restored?

online

logical. Process observations using an online algorithm.

Value

An object of the same class and dimension as x with the rolling and expanding sums.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
n <- 15
x <- rnorm(n)
weights <- 0.9 ^ (n:1)

# rolling sums with complete windows
roll_sum(x, width = 5)

# rolling sums with partial windows
roll_sum(x, width = 5, min_obs = 1)

# expanding sums with partial windows
roll_sum(x, width = n, min_obs = 1)

# expanding sums with partial windows and weights
roll_sum(x, width = n, min_obs = 1, weights = weights)

Example output

 [1]          NA          NA          NA          NA -1.07429503 -3.08441543
 [7] -2.20722899 -1.35045526 -1.15847871  0.94197323  1.54884019  2.63020157
[13]  0.31663308  0.39906049 -0.08741767
 [1]  1.43401919  0.66252949  0.83950107 -0.02768074 -1.07429503 -3.08441543
 [7] -2.20722899 -1.35045526 -1.15847871  0.94197323  1.54884019  2.63020157
[13]  0.31663308  0.39906049 -0.08741767
 [1]  1.43401919  0.66252949  0.83950107 -0.02768074 -1.07429503 -1.65039624
 [7] -1.54469949 -0.51095419 -1.18615945 -0.13232180 -0.10155605  1.08550208
[13] -0.19432111 -0.78709896 -0.21973947
 [1]  1.290617271  0.467214816  0.579767753 -0.258672647 -1.174758243
 [6] -1.575773511 -1.323069089 -0.260391405 -0.842036995  0.190620586
[11]  0.199247705  1.247675246 -0.028933151 -0.559539898  0.007037635

roll documentation built on July 13, 2020, 5:09 p.m.