# Table Computations - Difference Between Pair of Rows

This rule calculates the difference between two user-specified rows and displays it beneath the second row. 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 row spans.

## Example

## 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.

*OR*

- 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).

## JavaScript

```
includeWeb("Table JavaScript Utility Functions");
table.requireNumericTable();
var row_labels = table.rowLabels;
if (row_labels.length < 2)
form.ruleNotApplicable('table has only one row');
if (table.rowSpans.length > 0)
form.ruleNotApplicable('table has row spans');
// Set up controls for user input.
form.setHeading('The Difference Between a Pair of Rows');
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 combo_box_statistic = form.newComboBox('statistic', valid_statistics);
combo_box_statistic.setDefault(valid_statistics[0]);
var label_row_1 = form.newLabel('Row 1:');
var combo_box_row_1 = form.newComboBox('row1', row_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 row_labels_reduced = row_labels;
row_labels_reduced.splice(index_1, 1);
var label_row_2 = form.newLabel('Row 2:');
var combo_box_row_2 = form.newComboBox('row2', row_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_statistic = combo_box_statistic.requireValue(); // The statistic to use in the calculation
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.setOutputControls([label_output]);
form.setSummary('Creates a new row that is "' + row_label_1 + '" minus "' + row_label_2 + '"');
var first_row_index = table.rowIndex(row_label_1); //Specify the position of the first row (indices begin at 0)
var second_row_index = table.rowIndex(row_label_2); // Specify the position of the second row (indices begin at 0)
var last = second_row_index;
if (last < first_row_index) {
last = first_row_index;
}
var new_row_index = last + 1; // Index of the new row
insertRowAfterComplete(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.numberColumns; j++) {
stats[new_row_index][j] = stats[first_row_index][j] - stats[second_row_index][j];
}
// Assign the new statistics back to the table
table.set(target_statistic, stats);
```

## See also

- User Input for Rules for technical information on Rules.
- Rule Online Library for other examples of Rules.
- Table JavaScript and Plot JavaScript for the JavaScript that is used to write custom rules.
- JavaScript for information about the JavaScript programming language.