Missing Data - Save Variable(s) - Filter for Complete Cases
Jump to navigation
Jump to search
Creates a filter variable that includes cases with complete data (i.e., no missing values), based on the variables selected.
Example
The script produces a SUMMARY table with the new filter variable:
How to apply this QScript
- Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
- Click on the QScript when it appears in the QScripts and Rules section of the search results.
OR
- Select Automate > Browse Online Library.
- Select this QScript from the list.
Customizing the QScript
This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.
Customizing QScripts in Q4.11 and more recent versions
- Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
- Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
- Press Edit a Copy (bottom-left corner of the preview).
- Modify the JavaScript (see QScripts for more detail on this).
- Either:
- Run the QScript, by pressing the blue triangle button.
- Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.
Customizing QScripts in older versions
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('QScript Selection Functions');
includeWeb('QScript Utility Functions');
includeWeb('JavaScript Array Functions');
includeWeb('QScript Functions to Generate Outputs');
let is_displayr = inDisplayr();
let data_file;
let valid_filter_variables = [];
if (!is_displayr) {
// Selecting the variables
if (requireDataFile()) {
let one_data_set = project.dataFiles.length === 1;
data_file = one_data_set ? project.dataFiles[0] : selectOneDataFile('There is more than one data file in your ' +
'project. Select the data file to use:', project.dataFiles);
let applicable_variables = data_file.variables;
valid_selection_possible = applicable_variables.length > 0;
if (!valid_selection_possible) {
log('There are no appropriate data in the data file.');
}
while (valid_selection_possible && (!valid_filter_variables || valid_filter_variables.length < 1)) {
valid_filter_variables = selectManyVariablesByLabel('Select variables:', applicable_variables).variables;
}
}
}
else {
let valid_filter_variables = getAllUserSelections().selected_variables;
if (valid_filter_variables.length === 0) {
log('Please select variables to use this feature');
}
}
if (valid_filter_variables.length !== 0) {
data_file = valid_filter_variables[0].question.dataFile;
// Create variable
let expression = '(!isNaN(' + valid_filter_variables[0].name + ') || (typeof ' + valid_filter_variables[0].name + ' === \'string\' && ' + valid_filter_variables[0].name + '.trim().length > 0))';
let new_name = 'Complete cases: ' + valid_filter_variables[0].name;
let n_variables = valid_filter_variables.length;
if (n_variables > 1) {
for (let i = 1; i < n_variables; i++) {
expression += ' && (!isNaN(' + valid_filter_variables[i].name + ') || (typeof ' + valid_filter_variables[i].name + ' === \'string\' && ' + valid_filter_variables[i].name + '.trim().length > 0))';
new_name += ' + ' + valid_filter_variables[i].name;
}
}
let new_var = data_file.newJavaScriptVariable(expression, false, cleanVariableName(preventDuplicateVariableName(data_file, new_name)), new_name, null);
new_var.question.isFilter = true;
new_var.question.questionType = 'Pick One';
new_var.question.valueAttributes.setLabel(0, 'Incomplete');
new_var.question.valueAttributes.setLabel(1, 'Complete');
insertAtHoverButtonIfShown(new_var.question);
// Generating outputs
if (!is_displayr) {
reportNewRQuestion(new_var.question, 'Complete cases');
}
}
See also
- QScript for more general information about QScripts.
- QScript Examples Library for other examples.
- Online JavaScript Libraries for the libraries of functions that can be used when writing QScripts.
- QScript Reference for information about how QScript can manipulate the different elements of a project.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.