Skip to content

Rolling mean

Source code

Description

Compute the rolling (= moving) mean over the values in this array. A window of length window_size will traverse the array. The values that fill this window will (optionally) be multiplied with the weights given by the weight vector.

Usage

<Expr>$rolling_mean(
  window_size,
  weights = NULL,
  min_periods = NULL,
  ...,
  center = FALSE
)

Arguments

window_size Integer specifying the length of the window.
weights An optional slice with the same length as the window that will be multiplied elementwise with the values in the window.
min_periods The number of values in the window that should be non-null before computing a result. If NULL, it will be set equal to window size.
Ignored.
center Set the labels at the center of the window

Details

If you want to compute multiple aggregation statistics over the same dynamic window, consider using $rolling() this method can cache the window size computation.

Value

Expr

Examples

library(polars)

pl$DataFrame(a = c(1, 3, 2, 4, 5, 6))$
  with_columns(roll_mean = pl$col("a")$rolling_mean(window_size = 2))
#> shape: (6, 2)
#> ┌─────┬───────────┐
#> │ a   ┆ roll_mean │
#> │ --- ┆ ---       │
#> │ f64 ┆ f64       │
#> ╞═════╪═══════════╡
#> │ 1.0 ┆ null      │
#> │ 3.0 ┆ 2.0       │
#> │ 2.0 ┆ 2.5       │
#> │ 4.0 ┆ 3.0       │
#> │ 5.0 ┆ 4.5       │
#> │ 6.0 ┆ 5.5       │
#> └─────┴───────────┘