Skip to content

Slice the first n values of every sub-list

Description

Slice the first n values of every sub-list

Usage

<Expr>$list$head(n = 5L)

Arguments

n Number of values to return for each sub-list. Can be an Expr. Strings are parsed as column names.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  s = list(1:4, c(10L, 2L, 1L)),
  n = 1:2
)
df$with_columns(
  head_by_expr = pl$col("s")$list$head("n"),
  head_by_lit = pl$col("s")$list$head(2)
)
#> shape: (2, 4)
#> ┌─────────────┬─────┬──────────────┬─────────────┐
#> │ s           ┆ n   ┆ head_by_expr ┆ head_by_lit │
#> │ ---         ┆ --- ┆ ---          ┆ ---         │
#> │ list[i32]   ┆ i32 ┆ list[i32]    ┆ list[i32]   │
#> ╞═════════════╪═════╪══════════════╪═════════════╡
#> │ [1, 2, … 4] ┆ 1   ┆ [1]          ┆ [1, 2]      │
#> │ [10, 2, 1]  ┆ 2   ┆ [10, 2]      ┆ [10, 2]     │
#> └─────────────┴─────┴──────────────┴─────────────┘