Skip to content

Generate a range of integers

Source code

Description

Generate a range of integers

Usage

pl$int_range(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.
Not used.
dtype Data type of the range.

Value

An Expr with the data type specified in dtype (default is Int64).

See Also

pl$int_ranges() to generate a range of integers for each row of the input columns.

Examples

library("polars")

pl$int_range(0, 3) |>
  as_polars_series()
#> polars Series: shape: (3,)
#> Series: 'literal' [i64]
#> [
#>  0
#>  1
#>  2
#> ]
# "end" can be omitted for shorter syntax
pl$int_range(3) |>
  as_polars_series()
#> polars Series: shape: (3,)
#> Series: 'literal' [i64]
#> [
#>  0
#>  1
#>  2
#> ]
# custom data type
pl$int_range(3, dtype = pl$Int16) |>
  as_polars_series()
#> polars Series: shape: (3,)
#> Series: 'literal' [i16]
#> [
#>  0
#>  1
#>  2
#> ]
# one can use pl$int_range() and pl$len() to create an index column
df = pl$DataFrame(a = c(1, 3, 5), b = c(2, 4, 6))
df$select(
  index = pl$int_range(pl$len(), dtype = pl$UInt32),
  pl$all()
)
#> shape: (3, 3)
#> ┌───────┬─────┬─────┐
#> │ index ┆ a   ┆ b   │
#> │ ---   ┆ --- ┆ --- │
#> │ u32   ┆ f64 ┆ f64 │
#> ╞═══════╪═════╪═════╡
#> │ 0     ┆ 1.0 ┆ 2.0 │
#> │ 1     ┆ 3.0 ┆ 4.0 │
#> │ 2     ┆ 5.0 ┆ 6.0 │
#> └───────┴─────┴─────┘