roll_sum: Calculate the rolling sum of a _numeric_ vector, matrix, or...

View source: R/rutils.R

roll_sumR Documentation

Calculate the rolling sum of a numeric vector, matrix, or xts time series over a sliding window (lookback period).

Description

Calculate the rolling sum of a numeric vector, matrix, or xts time series over a sliding window (lookback period).

Usage

roll_sum(xtsv, lookb)

Arguments

xtsv

A vector, matrix, or xts time series containing one or more columns of data.

lookb

The size of the lookback window, equal to the number of data points for calculating the rolling sum.

Details

For example, if lookb=3, then the rolling sum at any point is equal to the sum of xtsv values for that point plus two preceding points. The initial values of roll_sum() are equal to cumsum() values, so that roll_sum() doesn't return any NA values.

The function roll_sum() performs the same operation as function runSum() from package TTR, but using vectorized functions, so it's a little faster.

Value

A vector, matrix, or xts time series with the same dimensions as the input series.

Examples

# Rolling sum of vector
vectorv <- rnorm(1000)
rutils::roll_sum(vectorv, lookb=3)
# Rolling sum of matrix
matrixv <- matrix(rnorm(1000), nc=5)
rutils::roll_sum(matrixv, lookb=3)
# Rolling sum of xts time series
xtsv <- xts(x=rnorm(1000), order.by=(Sys.time()-3600*(1:1000)))
rutils::roll_sum(xtsv, lookb=3)

algoquant/rutils documentation built on June 10, 2025, 3:55 p.m.