Add Table Statistic To Row Labels

From Q
Jump to navigation Jump to search

This code may be used to add a statistic from Statistics - Right to the row labels in your table. In this example, the Average value from Statistics - Right is being added to the row labels with specified text and fixed to 2 decimal places.

To use this snippet:

  1. Select your table.
  2. Select Automate > Custom Rule.
  3. Paste in the code from below.
  4. Click the 'Play' icon and close.
includeWeb('Table JavaScript Utility Functions');

// Set parameters and text
var statistic_to_add = "Average";
var decimals = 2;
var text_before = "Average =";
// Get labels and values
var vals = right_table.get(statistic_to_add);
var row_labs = table.rowLabels;
var new_labs = [];
// Loop through each row
for (var row = 0; row < table.numberRows; row++) {
    // Create string with old row label and value of specified statistic
    var val = parseFloat(vals[row]);
    var old_lab = row_labs[row];
    var new_lab = old_lab + " (" + text_before + " " + val.toFixed(decimals) + ")"
    new_labs[row] = new_lab;
}
// Set new row labels
table.rowLabels = new_labs;

See also