Modify Labels - Relabel Other/Specify Categories as Other

From Q
Jump to navigation Jump to search

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');


relabelOtherSpecifyInSelection();

function relabelOtherSpecifyInSelection() {
    
     // On the web just take from what is selected.
    const web_mode = inDisplayr();
    let selected_questions;
    let questions_without_other = []; // Selected questions that don't contain an Other
    let check_questions = []; // Questions which have more than one Other identified, which the user should check
    if (web_mode) {
        const displayr_fail_message = "Select a table, or one or more variable sets under Data Sets and run this option again."
        const user_selections = getAllUserSelections();
        selected_questions = user_selections.selected_questions.concat(user_selections.questions_in_rows_of_selected_tables);
        if (selected_questions.length == 0) {
            log(displayr_fail_message);
            return false;
        }
        let sorted_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, dataReductionHasOther);
        selected_questions = sorted_selection.applicable;
        questions_without_other = sorted_selection.notApplicable;
        if (selected_questions.length == 0) {
            log("None of the selected variable sets contain 'Other/Specify' categories to remove.")
            return false;
        }
    } else {
        let selected_datafiles = dataFileSelection();
         
        let candidate_questions = getAllQuestionsByTypes(selected_datafiles, 
            ["Pick One", "Pick One - Multi", "Pick Any", "Pick Any - Grid"]).filter(dataReductionHasOther);
         
        if (candidate_questions.length == 0) {
            log("There do not appear to be any questions with 'Other/Specify' categories in this project.");
            return false;
        }
        
        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;
        }
    }
        

    let other_questions = [];
    
    selected_questions.forEach(function (question) {
        relabelOtherCategoryInDataReduction(question, other_questions, check_questions);
    });


    // If running on the web, create a log
    if (web_mode) {
        if (check_questions.length > 0)
            log("Some variable sets contain more than one 'Other/Specify' options and should be updated manually:\r\n" 
                + check_questions.map(function (q) { return q.name; }).join("\r\n") + "\r\n");

        if (questions_without_other.length > 0)
            log("Could not modify because no \"Other\" categories were found:\r\n" 
                + questions_without_other.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 (check_questions.length > 0) {
            generateGroupOfSummaryTables("Questions which could not be relabeled", check_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 dataReductionHasOther(question) {
    if (["Experiment", "Ranking", "Text", "Text - Multi", "Date", "Number"].indexOf(question.questionType) > -1)
        return false;
    let data_reduction = question.dataReduction;
    let labels = data_reduction.rowLabels;
    let col_labels = data_reduction.columnLabels;
    if (col_labels != null)
        labels = labels.concat(col_labels);
    return labels.some(isOther);
}

function relabelOtherCategoryInDataReduction(question, other_questions, questions_with_duplicate_codes) {
    let data_reduction = question.dataReduction;
    let row_labels = data_reduction.rowLabels;
    let other_rows = row_labels.filter(isOther);

    let other_cols = [];
    let col_labels = data_reduction.columnLabels;
    if (col_labels != null) {
        other_cols = col_labels.filter(isOther);
    }

    // Can't rename duplicates, labels must be unique
    // Also, most normal questions will not have more
    // than one Other row or more than one Other column
    // so not safe to rename.
    if (other_rows.length > 1 
        || other_cols.length > 1 
        || arrayHasDuplicateElements(other_rows.concat(other_cols))) {
        questions_with_duplicate_codes.push(question);
        return;
    }

    other_rows.forEach(function (label) {
        data_reduction.rename(label, "Other");
    });

    other_cols.forEach(function (label) {
        data_reduction.rename(label, "Other");
    });

    other_questions.push(question);
}

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

See also