roll_apply: Apply rolling window statistics

View source: R/roll_apply.R

roll_applyR Documentation

Apply rolling window statistics

Description

Computes rolling statistics (mean, sd, median, sum, min, max, var) over a moving window of fixed length. Supports group-wise computation and various alignments.

Usage

roll_apply(data, cols = NULL, window = 3, method = "mean",
           align = "right", group = NULL, date_col = NULL,
           verbose = FALSE)

Arguments

data

A data frame, matrix, or numeric vector.

cols

Column indices or names to process.

window

Window size (number of observations).

method

Statistic to compute: "mean", "sd", "median", "sum", "min", "max", or "var".

align

Alignment of window: "right", "left", or "center".

group

Optional grouping column for group-wise rolling computation.

date_col

Not used. Present for interface consistency.

verbose

Logical; if TRUE, prints progress message.

Value

A data frame or vector with rolling statistics.

Examples

roll_apply(data[1:100, c(1, 4, 17:19)], cols = 3:5, window = 5, method = "mean")

dataprep documentation built on Oct. 1, 2026, 5:07 p.m.