Skip to content

Count unique values

Description

null is considered to be a unique value for the purposes of this operation.

Usage

<Expr>$n_unique()

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  x = c(1, 1, 2, 2, 3),
  y = c(1, 1, 1, NA, NA)
)
df$select(
  x_unique = pl$col("x")$n_unique(),
  y_unique = pl$col("y")$n_unique()
)
#> shape: (1, 2)
#> ┌──────────┬──────────┐
#> │ x_unique ┆ y_unique │
#> │ ---      ┆ ---      │
#> │ u32      ┆ u32      │
#> ╞══════════╪══════════╡
#> │ 3        ┆ 2        │
#> └──────────┴──────────┘