Modify Labels - Relabel Other/Specify Categories as Other

From Q
Jump to navigation Jump to search

Identifies variables in your project that look to contain 'Other/Specify' style categories and gives you the option to relabel these as 'Other'

This QScript identifies questions in your project that look to contain Other/Specify style categories and gives you the option to relabel these as 'Other'. You will be asked which questions you wish to relabel. Relabeled questions will be added to a folder in your report. If a question contains more than one category that looks like an Other/Specify category then the script will not relabel it. Such questions will be added to a separate folder for you to inspect.

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

// Renaming - Other Specify Categories as Other
 
includeWeb('QScript Selection Functions');
includeWeb('QScript Questionnaire Functions');
includeWeb('JavaScript Array Functions');
includeWeb('QScript Functions to Generate Outputs');


if (!main())
    log("QScript Canceled.");
else
    conditionallyEmptyLog("QScript Finished.");

function main() {
    
     // On the web just take from what is selected.
    var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());
    var selected_questions;
    var not_applicable_questions = [];
    if (web_mode) {
        selected_questions = project.report.selectedQuestions();
        var sorted_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, questionHasOtherCategory);
        selected_questions = sorted_selection.applicable;
        not_applicable_questions = sorted_selection.notApplicable;
    } else {
        var selected_datafiles = dataFileSelection();
         
        var candidate_questions = getAllQuestionsByTypes(selected_datafiles, ["Pick One", "Pick One - Multi", "Pick Any"]).filter(questionHasOtherCategory);
         
        if (candidate_questions.length == 0) {
            log("There do not appear to be any questions with 'Other/Specify' categories in this project.");
            return false;
        }
        
        var selected_questions = selectManyQuestions("The following questions have 'Other/Specify' categories. Please select questions to relabel: ", candidate_questions).questions;
        if (selected_questions.length == 0) {
            log('No questions selected.');
            return false;
        }
    }
        
    var num_questions = selected_questions.length;
    var question;
    var q_type;
    var other_questions = [];
    

    for (var j = 0; j < num_questions; j++) {
        question = selected_questions[j];
        q_type = question.questionType;
        if (q_type.indexOf("Pick One") == 0 || q_type == "Pick Any - Compact")
            tidyOtherValueLabel(question, other_questions, not_applicable_questions);
        else if (q_type == "Pick Any")
            tidyOtherVariableLabel(question, other_questions, not_applicable_questions);
    }

    // If running on the web, create a log
    if (web_mode) {
        if (other_questions.length > 0) {
            log("Modified:")
            log(other_questions.map(function (q) { return q.name; }).join("\r\n"));
        }

        if (other_questions.length > 0 && not_applicable_questions.length > 0)
            log("\r\n");

        if (not_applicable_questions.length > 0) {
            log("Could not modify:")
            log(not_applicable_questions.map(function (q) { return q.name; }).join("\r\n"));
        }
    } else {
        // Otherwise generate tables and a report
        if (other_questions.length > 0) {
            generateGroupOfSummaryTables("Questions with 'Other/Specify' categries relabeled", other_questions);
            log("Questions whose 'Other/Specify' categories have been relabeled have been added to the folder: Questions with 'Other/Specify' categries relabeled\r\n");
        }
        if (not_applicable_questions.length > 0) {
            generateGroupOfSummaryTables("Questions which could not be relabeled", not_applicable_questions);
            log("Some questions look like they contain more than one 'Other/Specify' category. These have not been relabeled. These questions have been added to the folder: Questions with more than one 'Other/Specify' label");
        }
    }

    return true;
}
 
 
function questionHasOtherCategory(question) {
    var q_type = question.questionType;
    if (q_type.indexOf("Pick One") == 0 || q_type == "Pick Any - Compact")
        return valueLabels(question).filter(isOther).length > 0;
    else if (q_type.indexOf("Pick Any") == 0)
        return getLabelsOfVariablesInArray(question.variables).filter(isOther).length > 0;
    else
        return false;
}
 
// Relables 'other' categories for Pick One, Pick One - Multi, and Pick Any - Compact questions
// If the input question contains a single 'other' category then it is relabeled
// and the question is added to the array other_questions.
// If the question contains more than one 'other' category then no relabeling is
// done and the question is added to an array possible_other_questions for the user
// to check.
function tidyOtherValueLabel(question, other_questions, possible_other_questions){
    var q_type = question.questionType;
    if (q_type.indexOf('Pick One') == 0 || q_type == "Pick Any - Compact") {
        var other_values = otherSourceValues(question);
        if (other_values.length > 0) {
            if (other_values.length == 1) {
                question.valueAttributes.setLabel(other_values[0],"Other");
                other_questions.push(question);
            } else {
                possible_other_questions.push(question);
            }
        }
    }
 
}
 
// Relabels 'other' variable labels for Pick Any questions
// If the input question contains a single 'other' category then it is relabeled
// and the question is added to the array other_questions.
// If the question contains more than one 'other' category then no relabeling is
// done and the question is added to an array possible_other_questions for the user
// to check.
function tidyOtherVariableLabel(question, other_questions, possible_other_questions) {
    var q_type = question.questionType;
    if (q_type == 'Pick Any') {
        var variables = question.variables;
        var labels = getLabelsOfVariablesInArray(variables);
        var other_label_indicator = labels.map(isOther);
        var num_others = arraySum(other_label_indicator);
        if (num_others == 1) {
            variables[other_label_indicator.indexOf(true)].label = "Other";
            other_questions.push(question);
        } else {
            possible_other_questions.push(question);
        }
    }
}

Prior to the 15th of December, 2015, this page was known as Renaming - Other Specify Categories as Other

See also