Modify Whole Table or Plot - Show Each Statistic in Separate Column
Jump to navigation
Jump to search
This rule re-arranges the numbers in the table so that statistics are shown in separate columns rather than above one another in each cell of the table. Significance highlighting from the original table will be shown in the cells for the first statistic. It is best to make any adjustments to the question in the brown drop-down from a separate table.
Example
This example applies the script to a crosstab between a question about Income and a banner that contains Age and Gender. The table is showing the Column % and n statistics.
Before:
After:
The two statistics are shown side-by-side.
Technical details
You can find a simpler version of this code, which does not contain the controls, here.
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
includeWeb('Table JavaScript Utility Functions');
var rule_name = "Show each statistic in a separate column";
form.setSummary(rule_name);
form.setHeading(rule_name);
form.setInputControls([form.newLabel('Statistics shown in separate columns.')]);
if (Q.fileFormatVersion() <= 8.62 && table.blueQuestion.questionType == "Pick One - Multi" && belowTableExists())
form.ruleNotApplicable("this rule is not currently compatible with this type of table. Please contact Q Support for a newer version");
// Store the initial state of the table
var initial_num_cols = table.numberColumns;
var initial_col_labels = table.columnLabels;
var initial_statistics = table.statistics;
// Determine which statistics will be used/displayed
// Column-comparisons type stats are strings rather than
// numbers and so can't be stored in the main part of the table.
// Such stats will be shown in the first column for each of the original column categories.
var column_comparison_stats = ["Column Comparisons", "Column Names", "Columns Compared"]
var table_stats_not_column_comparisons = initial_statistics.filter(function (stat) { return column_comparison_stats.indexOf(stat) == -1; });
if (table_stats_not_column_comparisons.length == 0)
form.ruleNotApplicable("there are only statistics related to the column comparisons on this table");
var target_stat = table_stats_not_column_comparisons[0];
var stats_on_final_table = [target_stat];
var num_stats = initial_statistics.length;
if (table.columnLabels != null && num_stats > 1) { // This table is two-dimensional and contains multiple statistics (excluding column comparisons type stats)
var initial_col_spans = table.columnSpans;
var initial_net_cols;
if (Q.fileFormatVersion() >= 8.22)
initial_net_cols = table.netColumns;
// Determine the new locations of the original columns
var original_columns_indices = [];
for (var j = 0; j < initial_num_cols; j++)
original_columns_indices.push(j);
original_columns_indices = original_columns_indices.map(function(x) { return num_stats*x; });
// Grab the stats and form new labels for the columns
var initial_stat_values = [];
for (var j = 0; j < num_stats; j++)
initial_stat_values[j] = table.get(initial_statistics[j]);
// Set the table to display a single main statistic
// (plus column-comparisons statistics if shown)
table.statistics = stats_on_final_table;
// Create new column labels
var new_col_labels = [];
for (var j = 0; j < initial_num_cols; j ++)
new_col_labels = new_col_labels.concat(initial_statistics);
// Expand the table
var new_num_cols = new_col_labels.length;
for (var j = 0; j < initial_num_cols; j++) {
var start_index = original_columns_indices[j];
for (var k = 0; k < num_stats - 1; k++)
table.insertColumnAfter(start_index, "");
}
// Expand the below_table if it's turned on
if (belowTableExists()) {
var is_1D = below_table.columnLabels == null;
for (var j = 0; j < initial_num_cols; j++) {
var start_index = original_columns_indices[j];
for (var k = 0; k < num_stats - 1; k++) {
if (is_1D)
below_table.insertRowAfter(start_index, "");
else
below_table.insertColumnAfter(start_index, "");
}
}
}
// Build new statistics table from original statistics
var new_stats = table.get(table.statistics[0]);
var cell_text = table.cellText;
for (var j = 0; j < initial_num_cols; j++) {
for (var k = 0; k < num_stats; k++) {
var cur_stats = initial_stat_values[k];
if (column_comparison_stats.indexOf(initial_statistics[k]) == -1) {
for (var row = 0; row < table.numberRows; row++)
new_stats[row][j*num_stats + k] = cur_stats[row][j];
} else {
for (var row = 0; row < table.numberRows; row++) {
new_stats[row][j*num_stats + k] = NaN;
cell_text[row][j*num_stats + k] = [cur_stats[row][j]];
}
}
}
}
table.columnLabels = new_col_labels;
table.set(target_stat, new_stats);
table.cellText = cell_text;
table.clearColumnSpans();
// Recombine the column labels as spans
// First the column labels
for (var j = 0; j < initial_col_labels.length; j++) {
var start = j*num_stats;
var end = start + num_stats;
var index_array = [];
for (var k = start; k < end; k++)
index_array.push(k);
table.spanColumns(index_array, initial_col_labels[j]);
}
// Now include all of the original spans
for (var j = 0; j < initial_col_spans.length; j++) {
var cur_inds = initial_col_spans[j].indices;
var start = cur_inds[0]*num_stats;
var end = cur_inds[cur_inds.length-1]*num_stats + num_stats;
var index_array = [];
for (var k = start; k < end; k++)
index_array.push(k);
table.spanColumns(index_array, initial_col_spans[j].label);
}
// Check if percentages option is turned on
if (table.showPercentSign) {
// Turn percentages off so we can specify which cells to show %
table.showPercentSign = false
var cell_texts = table.cellText;
// Loop through each cell and add percentages if % included in label
for (var row = 0; row < table.numberRows; row++)
for (var column = 0; column < table.numberColumns; column++) {
var col_label = table.columnLabels
if (col_label[column].indexOf("%")>-1) {
cell_texts[row][column] = ['%'];
}
}
table.cellText = cell_texts;
}
var version = Q.fileFormatVersion();
// Rename the statistic to reflect the other statistics shown
if (form.setTranslation)
form.setTranslation(target_stat, initial_statistics.join(", "));
if (version >= 8.22) {
// And keep track of the original NETs.
var new_nets = [];
initial_net_cols.forEach(function (col) {
for (var j = 0; j < num_stats; j++)
new_nets.push(num_stats*col + j);
});
table.netColumns = new_nets;
}
// Prevent hypothesis testing
table.hypothesisTestingEnabled = false;
table.showMissingAs('');
} else if (num_stats == 1)
form.ruleNotApplicable('only a single statistic is selected in the table');
else
form.ruleNotApplicable('statistics are already shown in separate columns on this table');
See also
- User Input for Rules for technical information on Rules.
- Rule Online Library for other examples of Rules.
- Table JavaScript and Plot JavaScript for the JavaScript that is used to write custom rules.
- JavaScript for information about the JavaScript programming language.