This article describes how to go from a crosstab...
...to a table where the Gender columns have been weighted by another question:
Requirements
This example uses the project called Phone 1.Q in Q's example projects. To access this:
- Select File > Open > Example Project.
- Open Phone 1.Q
The table we will use for this example is q4: Age in the Blue drop-down by BANNER in the Brown drop-down.
Method
1. Select your table.
2. Go to Automate > Custom Rule.
3. Paste the below into the dialog:
// 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.
}
}
The Gender columns now have been filtered by the q4 variable.
4. Press the Play button > Close > OK.
Note the following:
- 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.
Next
How to Make One Column Appear After Another in a Table
How to Change Row Labels in Tables