Count the occurrences of unique values
Description
Count the occurrences of unique values
Usage
<Expr>$value_counts(
...,
sort = FALSE,
parallel = FALSE,
name = NULL,
normalize = FALSE
)
Arguments
…
|
These dots are for future extensions and must be empty. |
sort
|
Sort the output by count in descending order. If FALSE
(default), the order of the output is random.
|
parallel
|
Execute the computation in parallel. This option should likely not be enabled in a group by context, as the computation is already parallelized per group. |
name
|
Give the resulting count field a specific name. If
normalize is TRUE it defaults to
“proportion” , otherwise it defaults to
“count” .
|
normalize
|
If TRUE , gives relative frequencies of the unique values.
|
Value
A polars expression
Examples
library("polars")
df <- pl$DataFrame(color = c("red", "blue", "red", "green", "blue", "blue"))
df$select(pl$col("color")$value_counts())
#> shape: (3, 1)
#> ┌─────────────┐
#> │ color │
#> │ --- │
#> │ struct[2] │
#> ╞═════════════╡
#> │ {"red",2} │
#> │ {"green",1} │
#> │ {"blue",3} │
#> └─────────────┘
# Sort the output by (descending) count and customize the count field name.
df <- df$select(pl$col("color")$value_counts(sort = TRUE, name = "n"))
df
#> shape: (3, 1)
#> ┌─────────────┐
#> │ color │
#> │ --- │
#> │ struct[2] │
#> ╞═════════════╡
#> │ {"blue",3} │
#> │ {"red",2} │
#> │ {"green",1} │
#> └─────────────┘
#> shape: (3, 2)
#> ┌───────┬─────┐
#> │ color ┆ n │
#> │ --- ┆ --- │
#> │ str ┆ u32 │
#> ╞═══════╪═════╡
#> │ blue ┆ 3 │
#> │ red ┆ 2 │
#> │ green ┆ 1 │
#> └───────┴─────┘