Skip to content

Gather every nth element in a list

Source code

Description

Gather every nth element in a list

Usage

<Expr>$list$gather_every(n, offset = 0)

Arguments

n Positive integer.
offset Starting index.

Value

Expr

Examples

library("polars")

df = pl$DataFrame(
  a = list(1:5, 6:8, 9:12),
  n = c(2, 1, 3),
  offset = c(0, 1, 0)
)

df$with_columns(
  gather_every = pl$col("a")$list$gather_every(pl$col("n"), offset = pl$col("offset"))
)
#> shape: (3, 4)
#> ┌───────────────┬─────┬────────┬──────────────┐
#> │ a             ┆ n   ┆ offset ┆ gather_every │
#> │ ---           ┆ --- ┆ ---    ┆ ---          │
#> │ list[i32]     ┆ f64 ┆ f64    ┆ list[i32]    │
#> ╞═══════════════╪═════╪════════╪══════════════╡
#> │ [1, 2, … 5]   ┆ 2.0 ┆ 0.0    ┆ [1, 3, 5]    │
#> │ [6, 7, 8]     ┆ 1.0 ┆ 1.0    ┆ [7, 8]       │
#> │ [9, 10, … 12] ┆ 3.0 ┆ 0.0    ┆ [9, 12]      │
#> └───────────────┴─────┴────────┴──────────────┘