Skip to content

Count how often a value produced occurs

Description

Count how often a value produced occurs

Usage

<Expr>$list$count_matches(element)

Arguments

element An expression that produces a single value.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(a = list(0, 1, c(1, 2, 3, 2), c(1, 2, 1), c(4, 4)))

df$with_columns(
  number_of_twos = pl$col("a")$list$count_matches(2)
)
#> shape: (5, 2)
#> ┌───────────────────┬────────────────┐
#> │ a                 ┆ number_of_twos │
#> │ ---               ┆ ---            │
#> │ list[f64]         ┆ u32            │
#> ╞═══════════════════╪════════════════╡
#> │ [0.0]             ┆ 0              │
#> │ [1.0]             ┆ 0              │
#> │ [1.0, 2.0, … 2.0] ┆ 2              │
#> │ [1.0, 2.0, 1.0]   ┆ 1              │
#> │ [4.0, 4.0]        ┆ 0              │
#> └───────────────────┴────────────────┘