table

Methods

Method Reference: table.groupcounts

table: G = groupcounts (T, groupvars)
table: G = groupcounts (T, groupvars, groupbins)
table: G = groupcounts (…, Name, Value)

Count the number of rows in each group of a table.

G = groupcounts (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, a GroupCount variable counting the rows in each group, and a Percent variable giving each group’s count as a percentage of the total. groupvars selects the grouping variables by name, index, logical vector, function handle, or vartype subscript.

The optional groupbins argument bins the grouping variables before grouping, using bin edges, a number of equal-width bins, a duration bin width, or a datetime calendar-unit keyword, or a cell array with one scheme per grouping variable. A binned grouping variable becomes a categorical and is renamed disc_<var> or <unit>_<var>. See groupsummary for details.

Groups are the sorted unique combinations of grouping values. 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 with a GroupCount of 0.
'IncludedEdge'
Either 'left' (the default) or 'right', selecting which edge of each bin is inclusive when groupbins is given.

Source Code: table

Example: 1

groupcounts tallies how many rows fall in each group — a frequency table. It also reports each group's share as a percentage of the total.

 Species = {'setosa'; 'virginica'; 'setosa'; 'virginica'; 'setosa'};
 T = table (Species)
T =
  5x1 table

       Species       
    _____________    

    {'setosa'   }    
    {'virginica'}    
    {'setosa'   }    
    {'virginica'}    
    {'setosa'   }
 groupcounts (T, 'Species')
ans =
  2x3 table

       Species       GroupCount    Percent    
    _____________    __________    _______    

    {'setosa'   }             3         60    
    {'virginica'}             2         40