Table Computations - Difference Between Pair of Columns

From Q
Jump to: navigation, search

This rule calculates the difference between two user-specified columns and displays it next to the second column. The statistic to be differenced can be chosen by the user when there is more than one statistic in the table. This script is not compatible with tables that have column spans.

Technical details

No significance tests are conducted on the computed difference.

How to apply this rule

For the first time in a project

  • Select the table(s)/chart(s) that you wish to apply the rule to.
  • Start typing the name of the Rule into the Search features and data box in the top right of the Q window.
  • Click on the Rule when it appears in the QScripts and Rules section of the search results.


  • Select Automate > Browse Online Library.
  • Choose this rule from the list.

Additional applications of the rule

  • Select a table or chart that has the rule and any table(s)/chart(s) that you wish to apply the rule to.
  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to apply.
  • Click on the Apply drop-down and choose your desired option.
  • Check New items to have it automatically applied to new items that you create. Use Edit > Project Options > Save as Template to create a new project template that automatically uses this rule.

Removing the rule

  • Select the table(s)/chart(s) that you wish to remove the rule from.
  • Press the Rules tab (bottom-right corner).
  • Press Apply next to the rule you wish to remove and choose the appropriate option.

How to modify the rule

  • Click on the Rules tab (bottom-left of the table/chart).
  • Select the rule that you wish to modify.
  • Click Edit Rule and make the desired changes. Alternatively, you can use the JavaScript below to make your own rule (see Customizing Rules).


includeWeb("Table JavaScript Utility Functions");
var column_labels = table.columnLabels;
if (column_labels == null || column_labels.length < 2)
    form.ruleNotApplicable('table has only one column');
if (table.columnSpans.length > 0)
    form.ruleNotApplicable('table has column spans');
// Set up controls for user input.
form.setHeading('The Difference Between a Pair of Columns');
var label_statistic = form.newLabel('Statistic to use:');
var valid_statistics = table.statistics.filter(function (statistic) {
    return statistic != 'Column Comparisons' && statistic != 'Column Names' &&
	statistic != 'Columns Compared';
if (valid_statistics.length == 0)
    form.ruleNotApplicable('there are no numeric statistics in this table.');
var valid_stats_translated = {
    try {
	return table.getTranslation(s);
    }catch(e) {
	return s;

var combo_box_statistic = form.newComboBox('statistic', valid_stats_translated);
var label_row_1 = form.newLabel('Column 1:');
var combo_box_row_1 = form.newComboBox('row1', column_labels);
form.setInputControls([label_statistic, combo_box_statistic, label_row_1, combo_box_row_1]);
var row_label_1 = combo_box_row_1.requireValue();
var index_1 = table.rowIndex(row_label_1);
var column_labels_reduced = column_labels;
column_labels_reduced.splice(index_1, 1);
var label_row_2 = form.newLabel('Column 2:');
var combo_box_row_2 = form.newComboBox('row2', column_labels_reduced);
form.setInputControls([label_statistic, combo_box_statistic, label_row_1, combo_box_row_1, label_row_2, combo_box_row_2]);
var row_label_2 = combo_box_row_2.requireValue();
var target_stat_translated = combo_box_statistic.requireValue(); // The statistic to use in the calculation
var target_statistic = valid_statistics[valid_stats_translated.indexOf(target_stat_translated)];
var new_row_label = row_label_1 + ' - ' + row_label_2; // Specify the label of the new row (containing the differences)
var label_output = form.newLabel(new_row_label);
form.setSummary('Creates a new column that is "' + row_label_1 + '" minus "' + row_label_2 + '"');
var first_column_index = table.columnIndex(row_label_1); //Specify the position of the first row (indices begin at 0)
var second_column_index = table.columnIndex(row_label_2); // Specify the position of the second row (indices begin at 0)
var last = second_column_index;
if (last < first_column_index) {
   last = first_column_index;
var new_row_index = last + 1; // Index of the new row
insertColumnAfterComplete(last, new_row_label); // Add a new row to store the differences
var stats = table.get(target_statistic); // Obtain the array of statistics
// Calculate the differences in the new row
for (var j = 0; j < table.numberRows; j++) {
    stats[j][new_row_index] = stats[j][first_column_index] - stats[j][second_column_index];
// Assign the new statistics back to the table
table.set(target_statistic, stats);

See also