Modify Cell Content - Blank Cells with Small Sample Sizes

From Q
Jump to navigation Jump to search


This rule makes cells appear blank and adds a footer to cells with sample sizes below the specified value of Column n.

Example

BlankingCellsRule.PNG

Technical details

Note this rule will only work on tables that contain 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.

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

let stat_name = table.getTranslation('Column n');
let grid_stat_name = table.getTranslation('Base n');
let stat_to_use = 'Column n';

if (table.availableStatistics.indexOf('Column n') == -1 && table.availableStatistics.indexOf('Base n') == -1)
    form.ruleNotApplicable('this format only applies to tables with the ' +
			   stat_name + ' or ' + grid_stat_name + ' statistics');

if (table.availableStatistics.indexOf('Column n') == -1){
    stat_name = grid_stat_name;
    stat_to_use = 'Base n';
}

// Set up controls for user input.
form.setHeading('Blank Cells With Small Sample Sizes:');
let description = form.newLabel("Prevent results from being shown when the column sample size falls below a specified value.");
description.lineBreakAfter = true;
let label = form.newLabel('Minimum ' + stat_name + ':');
let numeric_up_down  = form.newNumericUpDown('threshold');
numeric_up_down.setDefault(30);
numeric_up_down.setIncrement(1);
numeric_up_down.setMaximum(999999999);
numeric_up_down.lineBreakAfter = true;
let nan_checkbox = form.newCheckBox("cbnan", "Also set values of first table statistic to NaN");
nan_checkbox.setDefault(true);
form.setInputControls([description, label, numeric_up_down, nan_checkbox]);

let min_column_n = numeric_up_down.requireValue();
form.setSummary('Blank cells where ' + stat_name + ' < ' + min_column_n);
let primary_statistic = table.get(table.statistics[0]);
let blank_flag = false; // Keep track of whether any cells have been blanked.
let column_ns = table.get(stat_to_use);// Get column sample sizes.
for (let column = 0; column < table.numberColumns; column++) {// loop through the columns
    for (let row = 0; row < table.numberRows; row++) {
	// Get the sample size for this cell
        let column_n = column_ns[row][column];
        // If the column's sample size is less than min_column_n, make each cell in the column blank.
        if (column_n < min_column_n) {
            table.blankCell(row, column);
            primary_statistic[row][column] = NaN;
            blank_flag = true;
        }
    }
}

if (nan_checkbox.getValue())
    table.set(table.statistics[0], primary_statistic);

if (blank_flag && !isRTable()) {
    let extra_footers = table.extraFooters;// Store the extra footnotes we generate for each blank column.
    extra_footers.push('Blank cells have a sample size that is smaller than ' + min_column_n + '.');
    // Set the extra footnotes.
    table.extraFooters = extra_footers;
}

See also