groupcounts

Counts how many rows fall in each group of a table or vector.

Category: Language Fundamentals > Data Types

Syntax

G = groupcounts(A)

Counts the occurrences of each distinct value of a vector.

G = groupcounts(T, groupvars)

Counts rows of a table for each combination of the grouping variables.

Description

Groups are formed from the distinct values of the grouping variables and listed in sorted order. Percent is the share of all rows, in percent, and so sums to 100. For more statistics per group, such as means and sums, use groupsummary.

Input arguments

A
Numeric, string or categorical vector.
T
Table.
groupvars
Name(s) of the grouping variables.

Output arguments

G
For a table: a table with the group values, GroupCount and Percent. For a vector: the counts as a column.

Example

Rows per category

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

    g    GroupCount    Percent
    _    __________    _______

    a        3           60   
    b        1           20   
    c        1           20   
ans = 3×1

     2
     1
     3

See also

groupsummary findgroups unique histcounts accumarray