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.

Example

NaiveTotalsRows1.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 Row');
 
// Set up controls for user input.
form.setHeading('Computations - Add Naive Total For Each Row');
var sum_name_label = form.newLabel('New column 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")

if (table.columnLabels == null)
    form.ruleNotApplicable("this is a one-dimnesional table and does not have multiple columns")
 
var sum_name = sum_name_box.getValue();
 
// Insert a new column to store the numbers.
var last_column = table.numberColumns - 1;
insertColumnAfterComplete(last_column, 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_columns = getNotDuplicateColumns();
 
    // For each row...
    for (var row = 0; row < table.numberRows; row++) {
        if (typeof values[0][0] != "string") {        
            var sum = 0;
            // For each column...
            for (var column = 0; column < table.numberColumns; 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][table.numberColumns - 1] = sum;
        } else
            values[row][table.numberColumns - 1] = ""; // For column comparisons statistics
    }
    table.set(statistic, values);
});
 
 
function getNotDuplicateColumns() {
    var not_duplicate = table.get('Not Duplicate');
    var results = [];
    for (var i = 0; i < table.numberColumns; i++)
        for (var j = 0; j < table.numberRows; j++) {
            if (not_duplicate[j][i] == 1) {
                results.push(1);
                break;
            }
            if (j == table.numberRows - 1)
                results.push(0); // only run if all cells in the row are duplicate
        }
    return results;
}
 
// Returns true if Statistics - Below are available for this table.
// This function needs to be kept in the main body of the rule to ensure
// that below_table is included properly before the rule code is
// executed.
function belowTableExists() {
    var exists = true;
    try { 
        below_table.statistics;
    } catch (e) {
        exists = false;
    }
    return exists;
}

See also

Further reading: Data Analysis Software