Skip to content

Add a column for row indices

Source code

Description

Add a new column at index 0 that counts the rows

Usage

<LazyFrame>$with_row_index(name, offset = NULL)

Arguments

name string name of the created column
offset positive integer offset for the start of the counter

Value

A new LazyFrame with a counter column in front

Examples

library(polars)

df = pl$LazyFrame(mtcars)

# by default, the index starts at 0 (to mimic the behavior of Python Polars)
df$with_row_index("idx")
#> polars LazyFrame
#>  $describe_optimized_plan() : Show the optimized query plan.
#> 
#> Naive plan:
#> WITH ROW INDEX
#>   DF ["mpg", "cyl", "disp", "hp"]; PROJECT */11 COLUMNS; SELECTION: "None"
# but in R, we use a 1-index
df$with_row_index("idx", offset = 1)
#> polars LazyFrame
#>  $describe_optimized_plan() : Show the optimized query plan.
#> 
#> Naive plan:
#> WITH ROW INDEX
#>   DF ["mpg", "cyl", "disp", "hp"]; PROJECT */11 COLUMNS; SELECTION: "None"