summarise

Collapse each group of rows to a single row of named aggregations.

NoteExperimental

This function is experimental; its interface may change without notice.

Since 0.6.0.

With by: none the whole dataset is one group, yielding exactly one row; with by: ("col", ...) (a single string is accepted too) there is one output row per group, in first-appearance order. Each output row carries the grouping columns first (in by: order) then one column per aggregation. Every aggregation is a closure receiving that group’s rows (an array of dictionaries) and returning the cell value: n: rows => rows.len(), mean-hwy: rows => mean(rows.map(row => float(row.hwy))).y.

column. Names must not collide with a grouping column.

or an array of names.

Usage

summarise(
  data,
  ..aggregations,
  by: none,
)

Parameters

Parameter Default Description
data Row-store (array of dicts) or column-store (dict of arrays).
..aggregations Named closures name: rows => value, one per output
by none Grouping columns: none for the whole dataset, a column name,

Returns

A row-store array with one row per group.

Examples

Mean and count of hwy per drive train.

#let by-drv = summarise(
  mpg,
  n: rows => rows.len(),
  mean-hwy: rows => mean(rows.map(row => float(row.hwy))).y,
  by: "drv",
)

Whole-dataset summary (by: none) returns one row.

#let overall = summarise(mpg, n: rows => rows.len())

See also

count.

Back to top