Function Reference: grouptransform

datatypes: B = grouptransform (A, groupvars, method)
datatypes: B = grouptransform (A, groupvars, groupbins, method)
datatypes: [B, BG] = grouptransform (…)
datatypes: […] = grouptransform (…, Name, Value)

Transform the columns of an array group by group.

B = grouptransform (A, groupvars, method) groups the rows of the array A by the grouping variables groupvars, applies method to each column of A within each group, and returns B, the transformed values, one row per row of A and in the original order. groupvars is a grouping vector with one element per row of A, or a cell array of such vectors. Rows holding a missing value in a grouping variable form their own groups, which are transformed like any other group.

method is one of the names 'zscore', 'norm', 'meancenter', 'rescale', 'meanfill', 'linearfill', or a function handle. 'zscore' centers and scales each group to zero mean and unit standard deviation; 'norm' divides by the group 2-norm; 'meancenter' subtracts the group mean; 'rescale' rescales to the range [0, 1]; 'meanfill' replaces missing values with the group mean; and 'linearfill' fills missing values by linear interpolation within the group (leaving leading and trailing missing values unchanged). For the named methods NaN values are omitted when computing the group statistics. A function handle is applied to each group’s column slice and must return either a single row (broadcast) or one row per row of the group.

[B, BG] = grouptransform (…) also returns BG, the grouping values of each row. When groupvars is a single grouping vector, BG is that vector; when several grouping variables are given, BG is a cell array with one element per grouping variable.

The 'ReplaceValues' Name/Value pair (default true) controls whether the transformed values replace the columns of A or, when false, are appended to them. The optional groupbins argument bins the grouping variables before grouping (a vector of bin edges or a positive integer number of bins, or a cell array with one scheme per grouping variable); see groupsummary for details. The 'IncludedEdge' pair ('left' by default, or 'right') selects the inclusive bin edge.

To transform the variables of a table, call grouptransform (T, groupvars, …), which dispatches to the table method and returns the result as a table.

See also: groupsummary, groupfilter, findgroups, splitapply, table

Source Code: grouptransform