Skip to content

Count the number of unique values in every sub-array

Description

Count the number of unique values in every sub-array

Usage

<Expr>$arr$n_unique()

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  a = list(c(1, 1, 2), c(2, 3, 4))
)$cast(pl$Array(pl$Int64, 3))
df$with_columns(n_unique = pl$col("a")$arr$n_unique())
#> shape: (2, 2)
#> ┌───────────────┬──────────┐
#> │ a             ┆ n_unique │
#> │ ---           ┆ ---      │
#> │ array[i64, 3] ┆ u32      │
#> ╞═══════════════╪══════════╡
#> │ [1, 1, 2]     ┆ 2        │
#> │ [2, 3, 4]     ┆ 3        │
#> └───────────────┴──────────┘