Skip to content

Generate a range of integers for each row of the input columns

Description

Generate a range of integers for each row of the input columns

Usage

pl$int_ranges(start = 0, end = NULL, step = 1, ..., dtype = pl\$Int64)

Arguments

start Start of the range (inclusive). Defaults to 0.
end End of the range (exclusive). If NULL (default), the value of start is used and start is set to 0.
step Step size of the range.
These dots are for future extensions and must be empty.
dtype Data type of the range.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(start = c(1, -1), end = c(3, 2))
df$with_columns(int_range = pl$int_ranges("start", "end"))
#> shape: (2, 3)
#> ┌───────┬─────┬────────────┐
#> │ start ┆ end ┆ int_range  │
#> │ ---   ┆ --- ┆ ---        │
#> │ f64   ┆ f64 ┆ list[i64]  │
#> ╞═══════╪═════╪════════════╡
#> │ 1.0   ┆ 3.0 ┆ [1, 2]     │
#> │ -1.0  ┆ 2.0 ┆ [-1, 0, 1] │
#> └───────┴─────┴────────────┘
# end can be omitted for a shorter syntax$
df$select("end", int_range = pl$int_ranges("end"))
#> shape: (2, 2)
#> ┌─────┬───────────┐
#> │ end ┆ int_range │
#> │ --- ┆ ---       │
#> │ f64 ┆ list[i64] │
#> ╞═════╪═══════════╡
#> │ 3.0 ┆ [0, 1, 2] │
#> │ 2.0 ┆ [0, 1]    │
#> └─────┴───────────┘