Calculate the rolling sum of a numeric vector, matrix, or xts time series over a sliding window (lookback period).
A vector, matrix, or xts time series containing one or more columns of data.
The size of the lookback window, equal to the number of data points for calculating the rolling sum.
For example, if look_back=3, then the rolling sum at any point is
equal to the sum of
x_ts 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
roll_sum() performs the same operation as function
runSum() from package
using vectorized functions, so it's a little faster.
A vector, matrix, or xts time series with the same dimensions as the input series.
1 2 3 4 5 6 7 8 9
# Rolling sum of vector vec_tor <- rnorm(1000) rutils::roll_sum(vec_tor, look_back=3) # Rolling sum of matrix mat_rix <- matrix(rnorm(1000), nc=5) rutils::roll_sum(mat_rix, look_back=3) # Rolling sum of xts time series x_ts <- xts(x=rnorm(1000), order.by=(Sys.time()-3600*(1:1000))) rutils::roll_sum(x_ts, look_back=3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.