Modify Headers - Add Sample Size to Column Labels

From Q
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.



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.


  • 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).


You can find a simpler version of this code, which does not contain the controls, here.

includeWeb('Table JavaScript Utility Functions');


form.setHeading("Add Sample Size to Column Labels");
form.setSummary("Add sample size to 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');

let 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');

let description = form.newLabel('Add the ' + stat_name + ' to the column label for each column');
description.lineBreakAfter = true;

// Controls for changing the text and using the weighted sample size
let override_text_label = form.newLabel("Text to show in header:");
let override_text = form.newTextBox("ol");
override_text.setDefault("n = ");
override_text.lineBreakAfter = true;
let pop_box = form.newCheckBox('pop', "Show weighted sample size (Population)");
pop_box.lineBreakAfter = true;

let controls = [description, override_text_label, override_text, pop_box];
let label_text = override_text.getValue();
if (label_text == null)
    label_text = "";
let use_weighted = pop_box.getValue();

// If using population set rounding options
let num_decimals = 0;
if (use_weighted) {
    let decimals_label_before = form.newLabel("Round to:");
    let decimals_label_after = form.newLabel("decimal places");
    let decimal_control = form.newNumericUpDown('dec');
    controls = controls.concat([decimals_label_before, decimal_control, decimals_label_after]);
    num_decimals = decimal_control.getValue();

let stat_to_use = use_weighted ? 'Column Population' : 'Column n'

let 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];
let column_labels = table.columnLabels;// Get the standard column labels.
for (let column = 0; column < table.numberColumns; column++) {// For each 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