groupsummary

Summary statistics of table variables, computed per group.

Category: Language Fundamentals > Data Types

Syntax

G = groupsummary(T, groupvars, method)

Applies a statistic to every data variable, per group.

G = groupsummary(T, groupvars, method, datavars)

Restricts the statistic to the listed variables.

Description

groupsummary is the table-oriented way to do a split-apply-combine step. Several methods can be requested at once, each giving its own column. Use groupcounts when only the counts matter and grouptransform when the result should keep the shape of the input.

Input arguments

T
Table.
groupvars
Variable(s) that define the groups.
method
Name such as 'mean', 'sum', 'min', 'max', 'median', a cell array of several names, or a function handle.
datavars
Variable(s) to summarize.

Output arguments

G
Table with one row per group: the group values, GroupCount, and one column per statistic named method_variable, such as mean_v.

Example

Mean and max per group

tb = table(categorical({'a'; 'b'; 'a'; 'c'; 'a'}), [1; 2; 3; 4; 5], 'VariableNames', {'g', 'v'});
groupsummary(tb, 'g', {'mean', 'max'}, 'v')
Output
ans = 3×4 table

    g    GroupCount    mean_v    max_v
    _    __________    ______    _____

    a        3           3         5  
    b        1           2         2  
    c        1           4         4

See also

groupcounts grouptransform groupfilter findgroups splitapply varfun