table

Methods

Method Reference: table.groupsummary

table: G = groupsummary (T, groupvars)
table: G = groupsummary (T, groupvars, groupbins)
table: G = groupsummary (T, groupvars, groupbins, method)
table: G = groupsummary (T, groupvars, groupbins, method, datavars)
table: G = groupsummary (…, Name, Value)

Compute summary statistics by group for the variables of a table.

G = groupsummary (T, groupvars) groups the rows of the table T by the grouping variables groupvars and returns the table G with one row per group, holding the grouping variables and a GroupCount variable counting the rows in each group. groupvars selects the grouping variables by name, index, logical vector, function handle, or vartype subscript.

G = groupsummary (T, groupvars, method) also applies method to each data variable within each group and appends the results to G. method is one of the method names below, a function handle, or a cell array of method names andor function handles:

'sum', 'mean', 'median', 'mode'
'var', 'std', 'min', 'max'
'range', 'nnz'
Standard statistics, computed over numeric or logical data variables. NaN values are omitted (as in MATLAB) for every named method except 'nummissing'.
'nummissing'
The number of missing values in the group, supported for data variables of any type.
'numunique'
The number of unique non-missing values in the group, supported for data variables of any type.

A function handle is applied to each group’s slice of each data variable and must return a single row (its first dimension must be 1); it receives the values with NaN included.

G = groupsummary (T, groupvars, method, datavars) applies method only to the data variables selected by datavars (named, indexed, logical, function handle, or vartype subscript). By default every variable that is not a grouping variable is a data variable.

The computed variables of G are named <method>_<datavar>, e.g. mean_X; results from a function handle are named fun<n>_<datavar>, where n is the position of the handle among the requested methods. When several methods are requested the computed variables are ordered method first, then data variable.

The optional groupbins argument bins the grouping variables before grouping. A binning scheme is one of: a vector of bin edges; a positive integer number of equal-width bins spanning the data range; a duration scalar giving a fixed bin width (for a datetime or duration grouping variable); or, for a datetime grouping variable, a calendar-unit keyword ('second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year', 'decade', or 'century') that bins by that calendar period. A binned grouping variable becomes a categorical and is renamed disc_<var> for edge, bin-count, or width binning, or <unit>_<var> for calendar-unit binning. Pass a cell array with one scheme per grouping variable to bin them differently, or 'none' to leave a variable unbinned.

The following Name/Value pairs are accepted:

'IncludeMissingGroups'
A logical scalar. When true (the default), rows holding a missing value in a grouping variable form their own groups, sorted after the non-missing groups. When false, such rows are excluded.
'IncludeEmptyGroups'
A logical scalar, false by default. When true, the unused categories of a categorical or binned grouping variable contribute empty groups (GroupCount 0, 0 for 'sum' and 'nnz', NaN otherwise).
'IncludedEdge'
Either 'left' (the default) or 'right', selecting which edge of each bin is inclusive when groupbins is given.

Source Code: table

Example: 1

groupsummary is the one-call grouped aggregation: group by one or more variables, then apply a summary method to the data variables. It reports the group count alongside each requested statistic.

 Species = {'setosa'; 'virginica'; 'setosa'; 'virginica'; 'setosa'};
 Petal = [1.4; 5.1; 1.5; 5.9; 1.3];
 T = table (Species, Petal)
T =
  5x2 table

       Species       Petal    
    _____________    _____    

    {'setosa'   }      1.4    
    {'virginica'}      5.1    
    {'setosa'   }      1.5    
    {'virginica'}      5.9    
    {'setosa'   }      1.3
 groupsummary (T, 'Species', 'mean', 'Petal')
ans =
  2x3 table

       Species       GroupCount    mean_Petal    
    _____________    __________    __________    

    {'setosa'   }             3           1.4    
    {'virginica'}             2           5.5

Several methods can be requested at once.

 groupsummary (T, 'Species', {'min', 'max'}, 'Petal')
ans =
  2x4 table

       Species       GroupCount    min_Petal    max_Petal    
    _____________    __________    _________    _________    

    {'setosa'   }             3          1.3          1.5    
    {'virginica'}             2          5.1          5.9