rolling: Time-based rolling aggregates

rollingR Documentation

Time-based rolling aggregates

Description

Trailing rolling aggregates over a datetime window, usable inside mutate(). For each row, the aggregate covers the rows whose time falls in ⁠(time - every, time]⁠ – the row itself and everything within one window before it. With an upstream group_by(), windows are computed within each group. NA values are skipped.

Usage

roll_sum(x, time, every)

roll_mean(x, time, every)

roll_min(x, time, every)

roll_max(x, time, every)

roll_n(time, every)

Arguments

x

Value column to aggregate (not needed for roll_n).

time

Datetime column defining the window.

every

Window span as a fixed-width duration string.

Details

time must be a Date or POSIXct column; every is a fixed-width duration string ("15 min", "1 hour", "7 days") – calendar units (month, year) are not allowed because their length varies.

Value

A double column.

See Also

resample()

Examples

## Not run: 
tbl("readings.vtr") |>
  group_by(sensor) |>
  mutate(avg_1h = roll_mean(temp, t, "1 hour"),
         n_1h   = roll_n(t, "1 hour")) |>
  collect()

## End(Not run)


vectra documentation built on July 10, 2026, 5:08 p.m.