Skip to content

Compute aggregations for each group of a group by operation

Description

Compute aggregations for each group of a group by operation

Usage

<GroupBy>$agg(...)

Arguments

\<dynamic-dots\> Aggregations to compute for each group of the group by operation. Accepts expression input. Strings are parsed as column names.

Value

A polars DataFrame

Examples

library("polars")

# Compute the aggregation of the columns for each group.
df <- pl$DataFrame(
  a = c("a", "b", "a", "b", "c"),
  b = c(1, 2, 1, 3, 3),
  c = c(5, 4, 3, 2, 1)
)
df$group_by("a")$agg(pl$col("b"), pl$col("c"))
#> shape: (3, 3)
#> ┌─────┬────────────┬────────────┐
#> │ a   ┆ b          ┆ c          │
#> │ --- ┆ ---        ┆ ---        │
#> │ str ┆ list[f64]  ┆ list[f64]  │
#> ╞═════╪════════════╪════════════╡
#> │ c   ┆ [3.0]      ┆ [1.0]      │
#> │ a   ┆ [1.0, 1.0] ┆ [5.0, 3.0] │
#> │ b   ┆ [2.0, 3.0] ┆ [4.0, 2.0] │
#> └─────┴────────────┴────────────┘
# Compute the sum of a column for each group.
df$group_by("a")$agg(pl$col("b")$sum())
#> shape: (3, 2)
#> ┌─────┬─────┐
#> │ a   ┆ b   │
#> │ --- ┆ --- │
#> │ str ┆ f64 │
#> ╞═════╪═════╡
#> │ c   ┆ 3.0 │
#> │ a   ┆ 2.0 │
#> │ b   ┆ 5.0 │
#> └─────┴─────┘
# Compute multiple aggregates at once by passing a list of expressions.
df$group_by("a")$agg(pl$sum("b"), pl$col("c")$mean())
#> shape: (3, 3)
#> ┌─────┬─────┬─────┐
#> │ a   ┆ b   ┆ c   │
#> │ --- ┆ --- ┆ --- │
#> │ str ┆ f64 ┆ f64 │
#> ╞═════╪═════╪═════╡
#> │ c   ┆ 3.0 ┆ 1.0 │
#> │ a   ┆ 2.0 ┆ 4.0 │
#> │ b   ┆ 5.0 ┆ 3.0 │
#> └─────┴─────┴─────┘
# Use keyword arguments to easily name your expression inputs.
df$group_by("a")$agg(
  b_sum = pl$sum("b"),
  c_mean_squared = (pl$col("c") ** 2)$mean()
)
#> shape: (3, 3)
#> ┌─────┬───────┬────────────────┐
#> │ a   ┆ b_sum ┆ c_mean_squared │
#> │ --- ┆ ---   ┆ ---            │
#> │ str ┆ f64   ┆ f64            │
#> ╞═════╪═══════╪════════════════╡
#> │ a   ┆ 2.0   ┆ 17.0           │
#> │ c   ┆ 3.0   ┆ 1.0            │
#> │ b   ┆ 5.0   ┆ 10.0           │
#> └─────┴───────┴────────────────┘