Skip to content

Value counts

Source code

Description

Count all unique values and create a struct mapping value to count.

Usage

<Expr>$value_counts(..., sort = FALSE, parallel = FALSE, name, normalize = FALSE)

Arguments

Ignored.
sort Ensure the output is sorted from most values to least.
parallel Better to turn this off in the aggregation context, as it can lead to contention.
name Give the resulting count column a specific name. The default is “count” if normalize = FALSE and “proportion” if normalize = TRUE.
normalize If TRUE, it gives relative frequencies of the unique values instead of their count.

Value

Expr

Examples

library("polars")

df = as_polars_df(iris)
df$select(pl$col("Species")$value_counts())$unnest()
#> shape: (3, 2)
#> ┌────────────┬───────┐
#> │ Species    ┆ count │
#> │ ---        ┆ ---   │
#> │ cat        ┆ u32   │
#> ╞════════════╪═══════╡
#> │ setosa     ┆ 50    │
#> │ virginica  ┆ 50    │
#> │ versicolor ┆ 50    │
#> └────────────┴───────┘
df$select(pl$col("Species")$value_counts(normalize = TRUE))$unnest()
#> shape: (3, 2)
#> ┌────────────┬────────────┐
#> │ Species    ┆ proportion │
#> │ ---        ┆ ---        │
#> │ cat        ┆ f64        │
#> ╞════════════╪════════════╡
#> │ virginica  ┆ 0.333333   │
#> │ setosa     ┆ 0.333333   │
#> │ versicolor ┆ 0.333333   │
#> └────────────┴────────────┘