Automatically Applying Weights to Some Categories in a Banner
Jump to navigation
Jump to search
Refer to:
- Table JavaScript and Plot JavaScript for an explanation of how to run this code.
- Table JavaScript and Plot JavaScript Reference for technical information.
This example can be run using C:\Program Files\Q\Examples\Phone 1.Q (this file may be in a different place on your computer). Note in this example that:
- The weight variable that is automatically applied is not tagged as a weight and is not available in the Weight drop-down menu.
- This example only changes the values in the cells of the table. It will not update any statistics selected by Statistics - Below.
This script will cause significance results to be displayed incorrectly. Please read Caveats for Table JavaScript before using this script.
// Finds any columns in the span 'Gender' and applies the weight with variable name 'q4' to them.
var spans = table.columnSpans;
var columns = null;
for (var i=0; i<spans.length; i++)
if (spans[i].label == 'Gender')
columns = spans[i].indices;
var weighted_table = calculateTable(table.blue, table.brown,['!UseQFilters'], 'q4');
var stats = table.statistics; //gets the names of the statistics shown on the table
if (columns != null)
for (var i = 0; i < columns.length; i++) {
var column = columns[i];
// Copy the weighted statistics for the column to the current table .
for (var stat = 0; stat < stats.length; stat++) {
var weighted_values = weighted_table.get(stats[stat]);
var main_values = table.get(stats[stat]);
for (var row = 0; row < table.numberRows; row++)
main_values[row][column] = weighted_values[row][column];
table.set(stats[stat], main_values);// Set the altered values to the main table.
}
}
See also
- Table JavaScript and Plot JavaScript for an explanation of how to run this code.
- Table JavaScript and Plot JavaScript Reference for technical information.
- Table JavaScript and Plot JavaScript Examples Library for other examples.
- JavaScript for information about the JavaScript programming language.
- QScript for tools for automating projects using JavaScript.
- JavaScript Variables for detail on how to create new variables in the Variables and Questions tab using JavaScript.