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 = as_polars_lf(mtcars)

# by default, the index starts at 0 (to mimic the behavior of Python Polars)
df$with_row_index("idx")
#> polars LazyFrame
#>  $explain(): Show the optimized query plan.
#> 
#> Naive plan:
#> 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
#>  $explain(): Show the optimized query plan.
#> 
#> Naive plan:
#> ROW_INDEX
#>   DF ["mpg", "cyl", "disp", "hp"]; PROJECT */11 COLUMNS; SELECTION: None