Table Computations - Add Naive Total For Each Column

From Q
Jump to navigation Jump to search


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

Example

NaiveTotals1.PNG

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.

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

table.requireNumericTable();
includeWeb('Table JavaScript Utility Functions');

excludeRTables();

// Set up controls for user input.
form.setHeading('Add Naive Total For Each Column');
form.setSummary('Add naive total for each column');
let sum_name_label = form.newLabel('New row label:');
let sum_name_box = form.newTextBox('sn');
sum_name_box.setDefault('Total');
sum_name_box.lineBreakAfter = true;
let desc = form.newLabel('Adds a new row to the table which contains a total for each column.');
desc.lineBreakAfter = true;
let row_position = form.newComboBox('rowPos', ['Top', 'Bottom']);
row_position.setDefault('Bottom');
let position_label = form.newLabel('Position: ');
form.setInputControls([desc, sum_name_label, sum_name_box, position_label, row_position]);
if (table.availableStatistics.indexOf('Not Duplicate') === -1)
    form.ruleNotApplicable('the data in the table is not appropriate')

let sum_name = sum_name_box.getValue();

// Insert a new row to store the numbers.
let new_row_position = row_position.getValue();
let last_row = new_row_position === 'Bottom' ? table.numberRows - 1 : null;

insertRowAfterComplete(last_row, 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_rows = getNotDuplicateRows();
    let new_row_index = new_row_position === 'Bottom' ? table.numberRows - 1 : 0;
    let start_pos = new_row_position === 'Bottom' ? 0 : 1;
    let end_pos = table.numberRows - (new_row_position === 'Bottom');
    // For each column...
    for (let column = 0; column < table.numberColumns; column++) {
        if (typeof values[0][0] !== 'string') {
            let sum = 0;
            // For each row...
            for (let row = start_pos; row < end_pos; row++)
                // If this cell is not a duplicate...
                if (not_duplicates_rows[row] && !isNaN(values[row][column])) {
                    // Add its average value to this column's sum.
                    sum += values[row][column];
                }

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


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

// 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() {
    let exists = true;
    try {
        right_table.statistics;
    } catch (e) {
        exists = false;
    }
    return exists;
}

See also

Further reading: Data Analysis Software