Skip to content

Get the last n rows of each group

Description

Get the last n rows of each group

Usage

<LazyGroupBy>$tail(n = 5)

Arguments

n Number of rows to return.

Value

A polars LazyFrame

Examples

library("polars")

lf <- pl$LazyFrame(
  letters = c("c", "c", "a", "c", "a", "b"),
  nrs = 1:6
)
lf$collect()
#> shape: (6, 2)
#> ┌─────────┬─────┐
#> │ letters ┆ nrs │
#> │ ---     ┆ --- │
#> │ str     ┆ i32 │
#> ╞═════════╪═════╡
#> │ c       ┆ 1   │
#> │ c       ┆ 2   │
#> │ a       ┆ 3   │
#> │ c       ┆ 4   │
#> │ a       ┆ 5   │
#> │ b       ┆ 6   │
#> └─────────┴─────┘
lf$group_by("letters")$tail(2)$sort("letters")$collect()
#> shape: (5, 2)
#> ┌─────────┬─────┐
#> │ letters ┆ nrs │
#> │ ---     ┆ --- │
#> │ str     ┆ i32 │
#> ╞═════════╪═════╡
#> │ a       ┆ 3   │
#> │ a       ┆ 5   │
#> │ b       ┆ 6   │
#> │ c       ┆ 2   │
#> │ c       ┆ 4   │
#> └─────────┴─────┘