groupby

groupby(tbl,cols,ops) applies a reduction operation to table tbl based on the columns specified in cols and the operation(s) specified in ops.
Available operations: sum, prod, mean, min, max.

Examples

> tbl = table({'Boston'; 'San Diego'; 'Boston'}, [1500; 250; 100], 'VariableNames', {'Store', 'Sales'})#

      Store         Sales
     Boston          1500
  San Diego           250
     Boston           100

> groupby(tbl, 'Store', 'sum')#

      Store         Sales
     Boston          1600
  San Diego           250
> tbl = table({'May'; 'June'; 'May'}, {'Boston'; 'San Diego'; 'Boston'}, [1500; 250; 100], [4; 3; 3], 'VariableNames', {'Date', 'Store', 'Sales', 'People'})#

  Date      Store         Sales        People
   May     Boston          1500             4
  June  San Diego           250             3
   May     Boston           100             3

> groupby(tbl,{'Store','Date'},'sum')#

  Date      Store         Sales        People
   May     Boston          1600             7
  June  San Diego           250             3

> t = table({'Maths'; 'Phisics'; 'Maths'; 'Maths'; 'Phisics'}, {'Anne'; 'Peter'; 'Anne'; 'Peter'; 'Peter'}, [7; 6; 8; 9; 10], [0.1; 0.2; 0.1; 0.1; 0.2], 'VariableNames', {'Subject', 'Student', 'Exam', 'Points'})#

  Subject Student          Exam        Points
    Maths    Anne             7           0.1
  Phisics   Peter             6           0.2
    Maths    Anne             8           0.1
    Maths   Peter             9           0.1
  Phisics   Peter            10           0.2

> groupby(t,{'Subject','Student'},{'mean','sum'})#

  Subject Student          Exam        Points
    Maths    Anne           7.5           0.2
    Maths   Peter             9           0.1
  Phisics   Peter             8           0.4
	

Resources

See also

cellfun | sort | sortrows