Create New Variables - Flatten Question(s)

From Q
Jump to navigation Jump to search

Create a new Binary - Multi variable set by flattening the selected variable set This tool flattens Pick One - Multi questions into new Pick Any questionsNominal - Multi/Ordinal - Multi variable sets into new Binary - Multi variable sets while maintaining any changes made to the categories (e.g. merging or hiding).

Example

FlattenPickOneMulti4.PNG

In this example, the categories Hate and Dislike have been merged in the original questionvariable set and then the QScript has been used to create a flattened copy of the questionvariable set.

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 Functions for Combining Categories');
includeWeb('QScript Selection Functions');
includeWeb('QScript Table Functions');
includeWeb('QScript Functions to Generate Outputs');
includeWeb('QScript Value Attributes Functions');
includeWeb('JavaScript Array Functions');


main();
 
function main() {
    const allowed_types = ["Ordinal - Multi", "Nominal - Multi"];
    let questions_to_flatten = selectInputQuestions(allowed_types);
    if (!questions_to_flatten)
        return false;

    // Create new flattened questions
    let log_lines = [];
    let flattened_question_objects = flattenQuestionsKeepingDataReductionChanges(questions_to_flatten, log_lines);
    let flattened_questions = flattened_question_objects.map(function (obj) { return obj.newQuestion; });
    let log_joined = log_lines.join("\r\n");
 
    // For web mode, create a log
    if (inDisplayr()) {

        // Replace selected questions with flattened versions
        if (flattened_questions.length > 0)
            replaceQuestionsInSelectedTablesAndPlots(flattened_question_objects, true);    
        insertAtHoverButtonIfShown(flattened_questions); 
        if (log_lines.length > 0)
            log(log_joined);
 
        return true;
    }
 
    // Otherwise create a report
    if (flattened_questions.length > 0)
        reportNewRQuestion(flattened_questions, "Flattened questions");
    else
        log("There are no questions to flatten." + "\r\n\r\n" + log_joined);
     return true;
}
 
// For each of the input questions, work out the data reduction changes and then
// create a flattened version of the question which includes these changes.
// Return an array of pairs (original, flattened) for each question which could be flattened,
// and add a log message to log__contents for are question that has been flattened already.  
function flattenQuestionsKeepingDataReductionChanges(questions_to_flatten, log_contents) {
    let flattened_questions = [];
    questions_to_flatten.forEach(function (q) {
        let flattened_name = q.name + ' (flattened)';
        let q_flattened = q.dataFile.getQuestionByName(flattened_name);
 
        // Flatten the question according to the changes in the data reduction
        if (q_flattened != null) {
             log_contents.push(q.name + " has already been flattened: " + flattened_name); 
        } else {
            let value_attributes = q.valueAttributes;
            let non_missing_values = q.uniqueValues.filter(function (x) {
                return !value_attributes.getIsMissingData(x);
            }).sort();
 
            if (non_missing_values.length <= 1) {
                log_contents.push(q.name + " has only one unique non-missing value, so there is nothing to flatten.");
                return;
            }

            // Get the set of values for each code in the data reduction
            let merging_objects = getAllUnderlyingValues(q);
            // Filter out the set of values corresoponding to the NET as
            // we don't want a bunch of NETs in the flattened question
            merging_objects = merging_objects.filter(function (obj) {
                return obj.array.sort().toString() != non_missing_values.toString();
            });
            merging_objects = merging_objects.map(function (obj) {
                return { name: obj.label, values: obj.array };
            });

            q_flattened = pickOneMultiToPickAnyFlattenAndMergeByRows(q, merging_objects, true);
            setCountThisValueForVariablesInQuestion(q_flattened, 1, true);
            setCountThisValueForVariablesInQuestion(q_flattened, 0, false);
            q_flattened.needsCheckValuesToCount = false;
            flattened_questions.push({ originalQuestion: q, newQuestion: q_flattened });
        }
    });
    return flattened_questions;
}

See also