Missing Data - Save Variable(s) - Filter for Complete Cases

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

Completecases.PNG

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

  • Copy the JavaScript shown on this page.
  • Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
  • Modify the JavaScript (see QScripts for more detail on this).
  • Run the file using Automate > Run QScript (Macro) from File.

JavaScript

includeWeb('QScript Selection Functions');
includeWeb('QScript Utility Functions');
includeWeb('JavaScript Array Functions');
includeWeb('QScript Functions to Generate Outputs');

let is_displayr = inDisplayr();
variableIsApplicable = v => !v.variableType !== 'Text' && !v.question.isHidden;
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 non_text_variables = data_file.variables.filter(variableIsApplicable);
        valid_selection_possible = non_text_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:', non_text_variables).variables;
    }
} else {
    let all_selections = getAllUserSelections().selected_variables;
    let variables = splitArrayIntoApplicableAndNotApplicable(all_selections, variableIsApplicable);
    valid_filter_variables = variables.applicable;
    if (all_selections.length > 0 && valid_filter_variables.length === 0)
        log('Text or hidden variables are not appropriate to use in this feature. ' +
            'Please select other variables before rerun.');
    if (all_selections.length === 0)
        log('Please select any variables that are not text or hidden 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\'))';
    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\'))';
            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');

    // Generating outputs
    if (!is_displayr)
        reportNewRQuestion(new_var.question, 'Complete cases');
}

See also