Table Computations - Add Naive Total For Each Row

From Q
Jump to navigation Jump to search

This rule adds a new column to the right of the table which contains a total for each row. The total is calculated by adding up all of the statistic values in that row. This excludes any columns that are duplicates of other columns in the table (e.g. NETs). You will be able to specify the label of the new column.



Technical details

Statistical tests are not performed on these cells.

Any statistic shown on the table will be added up and included in the total, however be aware that for some statistics it may not be meaningful to display a naive total.

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');


// Set up controls for user input.
form.setHeading('Add Naive Total For Each Row');
form.setSummary('Add naive total for each row');
let sum_name_label = form.newLabel('New column label:');
let sum_name_box = form.newTextBox('sn');
sum_name_box.lineBreakAfter = true;
let desc = form.newLabel('Adds a new column to the table which contains a total for each row.');
desc.lineBreakAfter = true;
let column_position = form.newComboBox('columnPos', ['Left', 'Right']);
let position_label = form.newLabel('Position: ');

form.setInputControls([desc, sum_name_label, sum_name_box, position_label, column_position]);
if (table.availableStatistics.indexOf('Not Duplicate') === -1)
    form.ruleNotApplicable('the data in the table is not appropriate');

if (table.columnLabels == null)
    form.ruleNotApplicable('this is a one-dimnesional table and does not have multiple columns');

let sum_name = sum_name_box.getValue();

// Insert a new column to store the numbers.
let new_column_position = column_position.getValue();
let last_column = new_column_position === 'Right' ? table.numberColumns - 1 : null;
insertColumnAfterComplete(last_column, sum_name);

// Add a total for each statistic
table.statistics.forEach(function (statistic) {
    // Get the values for statistic
    let 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.
    let not_duplicates_columns = getNotDuplicateColumns();
    let new_column_index = new_column_position === 'Right' ? table.numberColumns - 1 : 0;
    let start_pos = new_column_position === 'Right' ? 0 : 1;
    let end_pos = table.numberColumns - (new_column_position === 'Right');
    // For each row...
    for (let row = 0; row < table.numberRows; row++) {
        if (typeof values[0][0] != 'string') {
            let sum = 0;
            // For each column...
            for (let column = start_pos; column < end_pos; column++)
                // If this column is not a duplicate...
                if (not_duplicates_columns[column] && !isNaN(values[row][column])) {
                    sum += values[row][column];

            // Store the sum in the new row that has been added
            values[row][new_column_index] = sum;
        } else
            values[row][new_column_index] = ''; // For column comparisons statistics
    table.set(statistic, values);

function getNotDuplicateColumns() {
    let not_duplicate = table.get('Not Duplicate');
    let results = [];
    for (let i = 0; i < table.numberColumns; i++)
        for (let j = 0; j < table.numberRows; j++) {
            if (not_duplicate[j][i] == 1) {
            if (j == table.numberRows - 1)
                results.push(0); // only run if all cells in the row are duplicate
    return results;

See also

Further reading: Data Analysis Software