Modify Headers - Add Sample Size to Column Labels
Jump to navigation
Jump to search
This rule adds the Column n values from the Statistics - Below to the bottom of the column headers in the table. This does not apply for tables that do not have the Statistics - Below available. You may choose to show the weighted sample sizes (Column Population) rather than the Column n.
Example
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
You can find a simpler version of this code, which does not contain the controls, here.
includeWeb('Table JavaScript Utility Functions')
form.setSummary("Adding Sample Size to the Column Labels");
if (table.numberColumns === 0)
form.ruleNotApplicable('the table has no columns');
if (table.numberColumns == 1)
form.ruleNotApplicable('the table only has a single column');
if (!belowTableExists())
form.ruleNotApplicable('the table does not have Statistics - Below');
var stat_name = table.getTranslation("Column n");
if (below_table.availableStatistics.indexOf('Column n') == -1)
form.ruleNotApplicable('the table does not have sample sizes (' + stat_name +
') available in the Statistics - Below');
// Controls for changing the text and using the weighted sample size
var override_text_label = form.newLabel("Text to show in header:");
var override_text = form.newTextBox("ol");
override_text.setDefault("n = ");
override_text.lineBreakAfter = true;
var pop_box = form.newCheckBox('pop', "Show weighted sample size (Population)");
pop_box.setDefault(false);
pop_box.lineBreakAfter = true;
var controls = [override_text_label, override_text, pop_box];
form.setInputControls(controls);
var label_text = override_text.getValue();
if (label_text == null)
label_text = "";
var use_weighted = pop_box.getValue();
// If using population set rounding options
var num_decimals = 0;
if (use_weighted) {
var decimals_label_before = form.newLabel("Round to:");
var decimals_label_after = form.newLabel("decimal places");
var decimal_control = form.newNumericUpDown('dec');
decimal_control.setDefault(0);
decimal_control.setIncrement(1);
decimal_control.setMaximum(5);
controls = controls.concat([decimals_label_before, decimal_control, decimals_label_after]);
form.setInputControls(controls);
num_decimals = decimal_control.getValue();
}
var stat_to_use = use_weighted ? 'Column Population' : 'Column n'
var column_ns = below_table.get(stat_to_use);// Get the column n values
if (use_weighted)
column_ns = roundStats(column_ns, num_decimals)
if (column_ns.length == 1)
column_ns = column_ns[0];
var column_labels = table.columnLabels;// Get the standard column labels.
for (var column = 0; column < table.numberColumns; column++) {// For each column...
var value = column_ns[column]
// Adjust the label to include the column name and column n, on new lines.
column_labels[column] = column_labels[column] + '\r\n' + label_text + column_ns[column];
}
// Set the adjusted column labels.
table.columnLabels = column_labels;
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.