Skip to content

Return the number of rows in each group

Source code

Description

Return the number of rows in each group

Usage

<LazyGroupBy>$len(name = NULL)

Arguments

name Assign a name to the resulting column. If NULL, defaults to “len”.

Value

A polars LazyFrame

Examples

library("polars")

lf <- pl$LazyFrame(
  a = c("Apple", "Apple", "Orange"),
  b = c(1, NA, 2)
)
lf$group_by("a")$len()$collect()
#> shape: (2, 2)
#> ┌────────┬─────┐
#> │ a      ┆ len │
#> │ ---    ┆ --- │
#> │ str    ┆ u32 │
#> ╞════════╪═════╡
#> │ Orange ┆ 1   │
#> │ Apple  ┆ 2   │
#> └────────┴─────┘
lf$group_by("a")$len("n")$collect()
#> shape: (2, 2)
#> ┌────────┬─────┐
#> │ a      ┆ n   │
#> │ ---    ┆ --- │
#> │ str    ┆ u32 │
#> ╞════════╪═════╡
#> │ Orange ┆ 1   │
#> │ Apple  ┆ 2   │
#> └────────┴─────┘