Modify Whole Table or Plot - Hide Columns with Small Sample Sizes

From Q
Jump to navigation Jump to search


This rule hides columns in the table in which all of the cells have a sample size that is smaller than a certain value.

See more details in [Whole Table or Plot - Hide Rows and Columns with Small Sample Sizes]

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

var __webpack_modules__ = ({});
// The module cache
var __webpack_module_cache__ = {};

// The require function
function __webpack_require__(moduleId) {

// Check if module is in cache
var cachedModule = __webpack_module_cache__[moduleId];
if (cachedModule !== undefined) {
return cachedModule.exports;
}
// Create a new module (and put it into the cache)
var module = (__webpack_module_cache__[moduleId] = {
exports: {}
});
// Execute the module function
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);

// Return the exports of the module
return module.exports;

}

// webpack/runtime/rspack_version
(() => {
__webpack_require__.rv = () => ("1.7.2")
})();
// webpack/runtime/rspack_unique_id
(() => {
__webpack_require__.ruid = "bundler=rspack@1.7.2";
})();
includeWeb("Table JavaScript Utility Functions");
includeWeb("JavaScript Array Functions");

form.setHeading("Hide Columns with Small Sample Sizes");

let description = form.newLabel("Hide any columns whose sample size is too small");
description.lineBreakAfter = true;

let sample_stats = ["Column n", "Base n", "Row n",
    "Column Population", "Base Population",
    "Row Population"].filter(function (stat) { return table.availableStatistics.indexOf(stat) > -1; });

let translated_sample_stats = translateStats(sample_stats);

if (sample_stats.length < 1) {
    form.ruleNotApplicable('table does not have any sample size statistics available');
}

// Create the form
let min_size_label = form.newLabel('Minimum Sample Size:');
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 min_sample = numeric_up_down.requireValue();

let controls = [description, min_size_label, numeric_up_down];

let column_label = form.newLabel("Statistic for columns:");
let stat_for_columns_box = form.newComboBox('cs', translated_sample_stats);
stat_for_columns_box.lineBreakAfter = true;
stat_for_columns_box.setDefault(translated_sample_stats[0]);

controls.push(column_label);
controls.push(stat_for_columns_box);

form.setInputControls(controls);

let summary_text = 'Remove columns where the sample size of all cells is less than ' + min_sample;

form.setSummary(summary_text);

let removed_columns;
let footers = table.extraFooters;

let col_stat = stat_for_columns_box.getValue();
col_stat = sample_stats[translated_sample_stats.indexOf(col_stat)];
let col_stat_values = table.get(col_stat);

let columns_to_remove = [];

for (let col = 0; col < table.numberColumns; col++) {
    if (allValuesBelowMinimum(col_stat_values.map(function (row_array) { return row_array[col]; }), min_sample))
        columns_to_remove.push(col);
}

let column_labels = table.columnLabels;
removed_columns = column_labels == null ? [] : columns_to_remove.map(function (x) { return column_labels[x]; });

// Remove empty columns
if (columns_to_remove.length > 0) {
    if (table.deleteColumns !== undefined) {
        table.deleteColumns(columns_to_remove);
    }
    else {
        for (let j = columns_to_remove.length - 1; j > -1; j--)
            deleteColumnComplete(columns_to_remove[j]);
    }
    footers.push("Some columns with sample sizes less than " + min_sample + " have been removed");
}

table.extraFooters = footers;

See also