AggregateRows

AggregateRows[tab,{key1f1,}]

computes different aggregation functions fi[tab] and assigns them to different keys keyi.

AggregateRows[tab,fspec,gspec]

forms groups by the distinct values given by gspec and then aggregates them using fspec.

AggregateRows[fspec]

represents an operator form for the two-argument version of AggregateRows.

AggregateRows[fspec,gspec]

represents an operator form for the three-argument version of AggregateRows.

Details

  • AggregateRows is also known as tabular reduce.
  • AggregateRows is typically used to summarize the data in groups of rows.
  • Possible forms of tab include:
  • Tabular[]type-consistent tabular data
    Dataset[]general hierarchical data
    {assoc1,assoc2,}list of associations with common keys
  • The application of AggregateRows does not change the form of tab.
  • For each group, there is a subtabular stab corresponding to a subset of rows of tab. The aggregation functions fi are applied to the association of columns given by FromTabular[stab,"Columns"].
  • Possible forms of grouping specification gspec include:
  • keygroups by distinct values associated with key
    newkeyggroups by distinct values of g[rowi], adding them as newkey
    {spec1,}groups by distinct values of all the speci together

Examples

open allclose all

Basic Examples  (2)

Take a Tabular object:

Aggregate all rows by computing the total of the values of column "col2":

Aggregate rows by first grouping those with common value of column "col1" and then totaling:

Aggregate a list of associations by computing a function of the values of key "a":

Aggregate associations by first grouping those with common value of key "b":

Scope  (8)

Input Data  (3)

Aggregate all rows of a Tabular object:

Aggregate all rows of a Dataset object:

Aggregate a list of associations:

Aggregation Functions  (2)

Take a Tabular object:

The aggregation function f receives an association with all columns, each one a list of values:

Use a pure function with named Slot notation to extract individual columns in the aggregation:

Alternatively, use Part notation:

Aggregate a Tabular object with a list of aggregation functions:

Mix the original columns in the computation:

Grouping Specifications  (3)

Aggregate rows by first grouping those with common value of column "col1" and then averaging:

Aggregate rows by grouping those with common value determined by the function g and then averaging:

Use a specific grouping function:

Take a Tabular object:

Aggregate rows by first grouping those with common values of pairs of columns "b" and "c":

Group by common values of pairs {"b"+"c","c"}:

Applications  (4)

Take a Tabular object with measurements of soil pH at three depth levels of 0, 30 and 80 meters:

Find mean pH for each depth:

Take measurements of various species of iris flower:

Compute the mean of the second column:

Compute the median of the "PetalLength" column:

Compute number of observations for each species:

Compute the mean of "SepalLength" for each species variety:

Old Faithful geyser data containing eruption times and next eruption waiting times in minutes:

Compute correlation between the columns:

Visualize the waiting time vs. duration:

Compute mean of the durations depending on the waiting time:

Convert into the normal form of the Tabular object:

Take a table of data about penguins in the Palmer Archipelago:

Extract column names:

Find the mean body weight for each sex:

Find the mean body weight for each island location:

Possible Issues  (1)

Using AggregateRows without grouping input may reduce a Tabular object too much:

Tally species varieties:

Use grouping:

Wolfram Research (2025), AggregateRows, Wolfram Language function, https://reference.wolfram.com/language/ref/AggregateRows.html.

Text

Wolfram Research (2025), AggregateRows, Wolfram Language function, https://reference.wolfram.com/language/ref/AggregateRows.html.

CMS

Wolfram Language. 2025. "AggregateRows." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/AggregateRows.html.

APA

Wolfram Language. (2025). AggregateRows. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/AggregateRows.html

BibTeX

@misc{reference.wolfram_2024_aggregaterows, author="Wolfram Research", title="{AggregateRows}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/AggregateRows.html}", note=[Accessed: 15-January-2025 ]}

BibLaTeX

@online{reference.wolfram_2024_aggregaterows, organization={Wolfram Research}, title={AggregateRows}, year={2025}, url={https://reference.wolfram.com/language/ref/AggregateRows.html}, note=[Accessed: 15-January-2025 ]}