Blanking Cells With Small Sample Sizes
Jump to navigation
Jump to search
When the sample size of a column is less than ten, make the cells in the column blank and adds a footer explaining what it has done. It will also deal with situations where missing data causes the sample size of each column to vary by row.
This example can be run in C:\Program Files\Q\Examples\Cola.Q (this may be located on a different place on your computer depending upon how Q was installed). Create a table with Q2. Gender in the blue drop-down menu and Preferred cola in the brown drop-down menu.
var min_sample_size = 10
var column_ns = table.get('Column n');// Get column sample sizes.
var extra_footers = table.extraFooters;// Store the extra footnotes we generate for each blank column.
for (var column = 0; column < table.numberColumns; column++) {// loop through the columns
for (var row = 0; row < table.numberRows; row++) {
// Get the sample size for this cell
var column_n = column_ns[row][column];
// If the column's sample size is less than min_sample_size, make each cell in the column blank.
if (column_n < min_sample_size) {
table.blankCell(row, column);
// Also, add a footnote that explains why some columns are missing.
extra_footers.push('Column "' + table.columnLabels[column]
+ " is blank because less than " + min_sample_size + " people were asked the question.");
}
}
}
// Set the extra footnotes.
table.extraFooters = extra_footers;
Note that:
- This example will only work on tables that contain Column n.
- To change the rule to reflect a sample size other than 10, replace 10 with your preferred minimum sample size.
See also
- Table JavaScript and Plot JavaScript for an explanation of how to run this code.
- Table JavaScript and Plot JavaScript Reference for technical information.
- Table JavaScript and Plot JavaScript Examples Library for other examples.
- JavaScript for information about the JavaScript programming language.
- QScript for tools for automating projects using JavaScript.
- JavaScript Variables for detail on how to create new variables in the Variables and Questions tab using JavaScript.