table

Methods

Method Reference: table.grouptransform

table: G = grouptransform (T, groupvars, method)
table: G = grouptransform (T, groupvars, groupbins, method)
table: G = grouptransform (…, method, datavars)
table: G = grouptransform (…, Name, Value)

Transform the data variables of a table group by group.

G = grouptransform (T, groupvars, method) groups the rows of the table T by the grouping variables groupvars, applies method to each data variable within each group, and returns the table G with the transformed values, one row per row of T and in the original order. groupvars selects the grouping variables by name, index, logical vector, function handle, or vartype subscript.

method is one of the transform names below or a function handle:

'zscore'
Center and scale each group to zero mean and unit standard deviation.
'norm'
Divide each group by its 2-norm.
'meancenter'
Subtract the group mean.
'rescale'
Rescale each group to the range [0, 1].
'meanfill'
Replace missing values with the group mean.
'linearfill'
Fill missing values by linear interpolation within the group; leading and trailing missing values are left unchanged.

For the named methods NaN values are omitted when computing the group statistics. A function handle is applied to each group’s slice of each data variable and must return either a single row (broadcast to all the group’s rows) or a result with one row per row of the group.

G = grouptransform (T, groupvars, method, datavars) transforms only the data variables selected by datavars (named, indexed, logical, function handle, or vartype subscript). By default every variable that is not a grouping variable is a data variable.

The following Name/Value pair is accepted:

'ReplaceValues'
A logical scalar. When true (the default), each data variable is replaced by its transformed values. When false, the transformed values are appended as new variables named <method>_<datavar> (fun1_<datavar> for a function handle), leaving the originals in place.
'IncludedEdge'
Either 'left' (the default) or 'right', selecting which edge of each bin is inclusive when groupbins is given.

Rows holding a missing value in a grouping variable form their own groups, which are transformed like any other group. 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; see groupsummary for details.

Source Code: table

Example: 1

grouptransform applies a group-wise transform but returns a table of the same height as the input — each value replaced by its transformed version. Here every petal length is centred on its own species mean.

 Species = {'setosa'; 'virginica'; 'setosa'; 'virginica'; 'setosa'};
 Petal = [1.4; 5.1; 1.5; 5.9; 1.3];
 T = table (Species, Petal)
T =
  5x2 table

       Species       Petal    
    _____________    _____    

    {'setosa'   }      1.4    
    {'virginica'}      5.1    
    {'setosa'   }      1.5    
    {'virginica'}      5.9    
    {'setosa'   }      1.3
 grouptransform (T, 'Species', @(x) x - mean (x), 'Petal')
ans =
  5x2 table

       Species          Petal        
    _____________    ____________    

    {'setosa'   }    -2.22045e-16    
    {'virginica'}            -0.4    
    {'setosa'   }             0.1    
    {'virginica'}             0.4    
    {'setosa'   }            -0.1