Compared with zoo::rollsum(), this function is implemented in C and is one magnitude faster.

rollsum(
  x,
  k,
  na_pad = FALSE,
  na.rm = FALSE,
  align = c("center", "left", "right")
)

Arguments

x

A numeric vector.

k

integer width of the rolling window.

na_pad

Logical value about whether to pad left/right ends with NAs.

na.rm

Similar to na.rm in base::sum(), a logical value indicating whether NA values should be stripped before the computation proceeds.

align

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

Value

The rolling sum.

See also

Examples


x <- 1:10

# Basic usage
rollsum(x, k = 3)
#> [1]  6  9 12 15 18 21 24 27

# Align at the right end, and use NA for padding
rollsum(x, k = 3, na_pad = TRUE, align = "right")
#>  [1] NA NA  6  9 12 15 18 21 24 27