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)

Arguments

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.

Value

Expr

Examples

library(polars)

df = pl$DataFrame(iris)$select(pl$col("Species")$value_counts())
df
#> shape: (3, 1)
#> ┌───────────────────┐
#> │ Species           │
#> │ ---               │
#> │ struct[2]         │
#> ╞═══════════════════╡
#> │ {"setosa",50}     │
#> │ {"virginica",50}  │
#> │ {"versicolor",50} │
#> └───────────────────┘
df$unnest()$to_data_frame() # recommended to unnest structs before converting to R
#>      Species count
#> 1     setosa    50
#> 2  virginica    50
#> 3 versicolor    50