Skip to content

vlt.table.factorPercentiles

  vlt.table.factorPercentiles - Compute percentile ranks of a variable within factor groups

  T = vlt.table.factorPercentiles(T, dataVar, factors)

  This function calculates the percentile rank (0-100) of a specified numeric
  variable (`dataVar`) within groups defined by the `factors`. The results are
  added to the table `T` in a new column.

  Inputs:
    T       - A MATLAB table.
    dataVar - A string or character vector specifying the name of the numeric
              variable in `T` for which to calculate percentiles.
    factors - A string array or cell array of character vectors specifying the
              variable names in `T` to group by.

  Outputs:
    T       - The input table with an additional column containing the percentile
              ranks. The new column name is constructed as:
              [dataVar '_' FACTOR1 '_' FACTOR2 ... '_PERCENTILE']
              where FACTOR1, etc., are the names of the grouping factors.

              The percentile rank is calculated using the formula:
              Percentile = (Rank / N) * 100
              where Rank is the rank of the value within its group (using average
              ranks for ties) and N is the number of non-NaN observations in the group.
              NaN values in the data variable are assigned a percentile of NaN.

  Example:
    % Create sample table
    T = table(['A';'A';'B';'B'], [1; 2; 10; 20], 'VariableNames', {'Group', 'Value'});

    % Calculate percentiles
    T = vlt.table.factorPercentiles(T, 'Value', "Group");

    % Resulting T has a new column 'Value_Group_PERCENTILE' with values [50; 100; 50; 100].