Computations - Add Naive Total For Each Column

From Q
Jump to: navigation, 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');
form.setSummary('Computations - Add Naive Total For Each Column');
 
// Set up controls for user input.
form.setHeading('Computations - Add Naive Total For Each Column');
var sum_name_label = form.newLabel('New row label:');
var sum_name_box = form.newTextBox("sn");
sum_name_box.setDefault('Total');
form.setInputControls([sum_name_label, sum_name_box]);
if (table.availableStatistics.indexOf("Not Duplicate") == -1)
    form.ruleNotApplicable("the data in the table is not appropriate")
 
var sum_name = sum_name_box.getValue();
 
// Insert a new row to store the numbers.
var last_row = table.numberRows - 1;
insertRowAfterComplete(last_row, sum_name);
 
// Add a total for each statistic
table.statistics.forEach(function (statistic) { 
    // 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_rows = getNotDuplicateRows();
 
    // For each column...
    for (var column = 0; column < table.numberColumns; column++) {
        if (typeof values[0][0] != "string") {        
            var sum = 0;
            // For each row...
            for (var row = 0; row < table.numberRows; 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[table.numberRows - 1][column] = sum;
        } else
            values[table.numberRows - 1][column] = ""; // For column comparisons statistics
    }
    table.set(statistic, values);
});
 
 
function getNotDuplicateRows() {
    var not_duplicate = table.get('Not Duplicate');
    var results = [];
    for (var i = 0; i < table.numberRows; i++)
        for (var 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() {
    var exists = true;
    try { 
        right_table.statistics;
    } catch (e) {
        exists = false;
    }
    return exists;
}

See also

Personal tools
Namespaces

Variants
Actions
Navigation
Categories
Toolbox