Skip to content

Return the mean per group

Description

Return the mean per group

Usage

<LazyGroupBy>$mean()

Value

A polars LazyFrame

Examples

library("polars")

lf <- pl$LazyFrame(
  grp = c("c", "c", "a", "c", "a", "b"),
  x = c(0.5, 0.5, 4, 10, 13, 14),
  y = 1:6,
  z = c(TRUE, TRUE, FALSE, TRUE, FALSE, TRUE)
)
lf$collect()
#> shape: (6, 4)
#> ┌─────┬──────┬─────┬───────┐
#> │ grp ┆ x    ┆ y   ┆ z     │
#> │ --- ┆ ---  ┆ --- ┆ ---   │
#> │ str ┆ f64  ┆ i32 ┆ bool  │
#> ╞═════╪══════╪═════╪═══════╡
#> │ c   ┆ 0.5  ┆ 1   ┆ true  │
#> │ c   ┆ 0.5  ┆ 2   ┆ true  │
#> │ a   ┆ 4.0  ┆ 3   ┆ false │
#> │ c   ┆ 10.0 ┆ 4   ┆ true  │
#> │ a   ┆ 13.0 ┆ 5   ┆ false │
#> │ b   ┆ 14.0 ┆ 6   ┆ true  │
#> └─────┴──────┴─────┴───────┘
lf$group_by("grp")$mean()$collect()
#> shape: (3, 4)
#> ┌─────┬──────────┬──────────┬─────┐
#> │ grp ┆ x        ┆ y        ┆ z   │
#> │ --- ┆ ---      ┆ ---      ┆ --- │
#> │ str ┆ f64      ┆ f64      ┆ f64 │
#> ╞═════╪══════════╪══════════╪═════╡
#> │ c   ┆ 3.666667 ┆ 2.333333 ┆ 1.0 │
#> │ a   ┆ 8.5      ┆ 4.0      ┆ 0.0 │
#> │ b   ┆ 14.0     ┆ 6.0      ┆ 1.0 │
#> └─────┴──────────┴──────────┴─────┘