Skip to content

Get the first n rows of each group

Description

Get the first n rows of each group

Usage

<GroupBy>$head(n = 5)

Arguments

n Number of rows to return.

Value

A polars DataFrame

Examples

library("polars")

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