table

Methods

Method Reference: table.pivot

table: P = pivot (T, 'Columns', colvars)
table: P = pivot (T, 'Rows', rowvars)
table: P = pivot (…, Name, Value)

Summarize tabular data in a pivoted table.

P = pivot (T, 'Columns', colvars, 'Rows', rowvars) reshapes the table T into the pivoted table P. The unique combinations of the grouping variables colvars become the variables (columns) of P, the unique combinations of the grouping variables rowvars become its rows, and each cell holds one statistic computed over the rows of T that fall into that row-and-column group. At least one of 'Columns' or 'Rows' is required; an omitted dimension collapses to a single group. Each of colvars and rowvars selects variables by name, index, or logical vector, and may name several variables.

Groups are the sorted unique combinations of the grouping values, with the first variable varying slowest; a categorical variable groups by its category order. Column variable names are taken from the grouping values (e.g. 'true'/'false' for a logical variable), joined with '_' when several variables define the columns.

The following Name/Value pairs are accepted:

'DataVariable'
The single variable whose values are aggregated. When omitted, the cells hold group counts.
'Method'
The aggregation applied to 'DataVariable': one of 'count', 'sum', 'mean', 'median', 'mode', 'std', 'var', 'min', 'max', 'range', 'nummissing', 'numunique', 'nnz', 'percentage', 'none', or a function handle. Named methods omit missing values. The default is 'count' when no data variable is given or the data variable is non-numeric, and 'sum' when it is numeric. 'none' rearranges the data without aggregating and requires at most one value per cell.
'IncludeMissingGroups'
A logical scalar, true by default. When true, rows holding a missing value in a grouping variable form their own group, sorted last; when false, such rows are excluded.
'IncludeEmptyGroups'
A logical scalar, false by default. When true, every category of a categorical grouping variable contributes a group even if it is unused in the data, so unused combinations appear as empty cells.
'IncludeTotals'
A logical scalar, false by default. When true, a 'Total' marginal row and/or column holding the same statistic computed over each margin is appended. Row labels are then placed in the row names.
'RowLabelPlacement'
Either 'variable' (the default), which keeps the row grouping variables as the leftmost variables of P, or 'rownames', which places the row group labels in the RowNames property.
'ColumnsBinMethod', 'RowsBinMethod'
A binning scheme applied to the 'Columns' or 'Rows' grouping variables before pivoting: a vector of bin edges, a number of equal-width bins, a duration bin width, or a datetime calendar-unit keyword (see groupsummary), or a cell array with one scheme per variable. Each binned variable becomes a categorical. The default 'none' applies no binning.
'IncludedEdge'
Either 'left' (the default) or 'right', selecting which edge of each bin is inclusive when a binning scheme is given.
'OutputFormat'
'flat' (default) names each output column after the joined column grouping values (lvl_lvl). 'nested' instead groups two or more 'Columns' variables into nested tables: one outer variable per level of the first column grouping variable, each a nested table whose variables are the next grouping variable’s levels (recursively). A marginal-total column, if any, stays a flat outer variable.

Source Code: table

Example: 1

pivot builds a cross-tabulation. With only 'Rows' it counts the rows in each group — a one-way frequency table.

 Region = {'N'; 'N'; 'S'; 'S'; 'N'; 'S'};
 Quarter = categorical ({'Q1'; 'Q2'; 'Q1'; 'Q2'; 'Q1'; 'Q2'});
 Sales = [10; 20; 30; 40; 15; 25];
 T = table (Region, Quarter, Sales)
T =
  6x3 table

    Region    Quarter    Sales    
    ______    _______    _____    

    {'N' }         Q1       10    
    {'N' }         Q2       20    
    {'S' }         Q1       30    
    {'S' }         Q2       40    
    {'N' }         Q1       15    
    {'S' }         Q2       25
 pivot (T, 'Rows', 'Region')
ans =
  2x2 table

    Region    count    
    ______    _____    

    {'N' }        3    
    {'S' }        3

Example: 2

Add 'Columns' to spread a second grouping variable across the columns, and aggregate a data variable in each cell — here total Sales by region and quarter.

 Region = {'N'; 'N'; 'S'; 'S'; 'N'; 'S'};
 Quarter = categorical ({'Q1'; 'Q2'; 'Q1'; 'Q2'; 'Q1'; 'Q2'});
 Sales = [10; 20; 30; 40; 15; 25];
 T = table (Region, Quarter, Sales)
T =
  6x3 table

    Region    Quarter    Sales    
    ______    _______    _____    

    {'N' }         Q1       10    
    {'N' }         Q2       20    
    {'S' }         Q1       30    
    {'S' }         Q2       40    
    {'N' }         Q1       15    
    {'S' }         Q2       25
 pivot (T, 'Rows', 'Region', 'Columns', 'Quarter', ...
        'DataVariable', 'Sales', 'Method', 'sum')
ans =
  2x3 table

    Region    Q1    Q2    
    ______    __    __    

    {'N' }    25    20    
    {'S' }    30    65