# Computations - The Average of Each Column

From Q

This rule adds an extra row to the bottom of the table which shows the average of the statistics that are shown in the columns. Rows which are duplicates of other rows, for example the *NET* row, are not included in the computation of the average. If there is more than one statistic in the table, the user may select the statistic to use.

## Example

## Technical details

Statistical tests are not performed on these cells. Averaging is not performed on the statistics shown in Statistics - Right.

## 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();
form.setSummary('Computing the Average for each Column');
// Set up controls for user input.
includeWeb('Table JavaScript Utility Functions');
table.requireNumericTable();
form.setSummary('Computing the Average for each Column');
// Set up controls for user input.
form.setHeading('Computing the Average for each Column');
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.');
if (table.numberRows == 1)
form.ruleNotApplicable('there is a single row on this table.');
if (table.availableStatistics.indexOf('Not Duplicate') == -1)
form.ruleNotApplicable('the data in this table is not appopriate');
var combo_box = form.newComboBox('statistic', valid_statistics);
combo_box.setDefault(valid_statistics[0]);
form.setInputControls([label_statistic, combo_box]);
var statistic = combo_box.getValue();
// Insert a new row to store the numbers.
var last_row = table.numberRows - 1;
insertRowAfterComplete(last_row, 'Column Average')
// Get the values for statistic
var values = table.get(statistic);
// Get the not duplicate markers for each cell. This
// indicates whether the cell has been copied from
// another cell, such as when NETs are created.
var not_duplicates = table.get('Not Duplicate');
var pick_one_multi_vars_across_rows = table.blueQuestion.questionType == "Pick One - Multi" && table.blueQuestion.dataReduction.transposed;
// For each column...
for (var column = 0; column < table.numberColumns; column++) {
// Keep track of the sum of averages, and the number of
// non-duplicate rows so far.
var sum = 0;
var unique_rows = 0;
// For each row...
for (var row = 0; row < table.numberRows; row++)
// If this cell is not a duplicate...
if (!isNaN(not_duplicates[row][column]) && (not_duplicates[row][column] || pick_one_multi_vars_across_rows) && !isNaN(values[row][column])) {
// Add its average value to this column's sum.
sum += values[row][column];
// And increase the count of non-duplicate rows.
unique_rows++;
}
// We now have a sum of all the averages in this column.
// Compute the average.
var average = NaN;
if (unique_rows > 0)
average = sum / unique_rows;
// Store the average in the new 'Column Average' row
// we added above.
values[table.numberRows - 1][column] = average;
}
// Use our new average values.
table.set(statistic, values);
// Returns true if Statistics - Right are available for this table.
// This function needs to be kept in the main body of the rule to ensure
// that right_table is included properly before the rule code is
// executed.
function rightTableExists() {
var exists = true;
try {
right_table.statistics;
} catch (e) {
exists = false;
}
return exists;
}
```

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

Q Technical Reference

Q Technical Reference

Q Technical Reference > Setting Up Data > Creating New Variables

Q Technical Reference > Updating and Automation > Automation Online Library

Q Technical Reference > Updating and Automation > JavaScript > Table JavaScript and Plot JavaScript

Rule Online Library