Skip to content

Shift values

Source code

Description

Shift values

Usage

<Expr>$shift(periods = 1)

Arguments

periods Number of periods to shift, may be negative.

Value

Expr

Examples

library(polars)

pl$DataFrame(a = c(1, 2, 4, 5, 8))$
  with_columns(
  pl$col("a")$shift(-2)$alias("shift-2"),
  pl$col("a")$shift(2)$alias("shift+2")
)
#> shape: (5, 3)
#> ┌─────┬─────────┬─────────┐
#> │ a   ┆ shift-2 ┆ shift+2 │
#> │ --- ┆ ---     ┆ ---     │
#> │ f64 ┆ f64     ┆ f64     │
#> ╞═════╪═════════╪═════════╡
#> │ 1.0 ┆ 4.0     ┆ null    │
#> │ 2.0 ┆ 5.0     ┆ null    │
#> │ 4.0 ┆ 8.0     ┆ 1.0     │
#> │ 5.0 ┆ null    ┆ 2.0     │
#> │ 8.0 ┆ null    ┆ 4.0     │
#> └─────┴─────────┴─────────┘