Function Reference: groupsummary

datatypes: B = groupsummary (A, groupvars, method)
datatypes: B = groupsummary (A, groupvars, groupbins, method)
datatypes: [B, BG, BC] = groupsummary (…)

Compute summary statistics by group for an array.

B = groupsummary (A, groupvars, method) groups the rows of the array A by the grouping variables groupvars and applies method to each column of A within each group, returning the results in B, which has one row per group. groupvars is a grouping vector with one element per row of A, or a cell array of such vectors. Groups are the sorted unique combinations of grouping values; rows holding a missing value in a grouping variable form their own groups, sorted after the non-missing groups.

method is one of the names 'sum', 'mean', 'median', 'mode', 'var', 'std', 'min', 'max', 'range', 'nnz', 'nummissing', or 'numunique', a function handle, or a cell array of method names andor function handles. NaN values are omitted for every named method except 'nummissing'; a function handle receives the values with NaN included and must return a single row. When several methods are requested the columns of B are ordered by column of A first, then by method.

[B, BG, BC] = groupsummary (…) also returns BG, the grouping values that identify each group, and BC, a column vector with the number of rows in each group. When groupvars is a single grouping vector, BG holds its representative value for each group; when several grouping variables are given, BG is a cell array with one element per grouping variable.

The optional groupbins argument bins the grouping variables before grouping: a vector of bin edges or a positive integer number of equal-width bins, applied to a numeric, datetime, or duration grouping variable, or a cell array with one scheme per grouping variable. Each binned variable becomes a categorical of bin interval labels.

The behaviour can be modified with the 'IncludeMissingGroups' (default true), 'IncludeEmptyGroups' (default false), and 'IncludedEdge' (default 'left', the inclusive bin edge) Name/Value pairs, as for the table method. When 'IncludeEmptyGroups' is true, the unused categories of a categorical or binned grouping variable contribute empty groups.

To summarise the variables of a table, call groupsummary (T, groupvars, …), which dispatches to the table method and returns the result as a table.

See also: findgroups, splitapply, table

Source Code: groupsummary