Modify Data - Merge and Flatten Scales

From Q
Jump to navigation Jump to search

This QScript creates merges 5, 6, 7, 9, 10 and 11-point scales into 3 categories, and restructures the data, so that the data is flattened (i.e., in a single column of numbers, so it can be more readily crosstabbed).

Example

MergeAndFlatten.png

Technical details

This QScript:

  1. Searches through all the questions in the project and identifies any Pick One - Multi questions containing 5, 6, 7, 9, 10 or 11 point scales.
  2. Restructures the data into a Pick Any question, with three combined categories. Refer to the JavaScript below for the specific details.
  3. Produces a new folder in your Report showing the new questions.

This QScript relies on the values in the scale question matching the scale points, so that a 5-point scale would have values 1, 2, 3, 4, and 5.

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');
  
// Specify the types of mergings that are performed for different scales 
var mergings_by_points = [{
        points: 5, mergings : [
        {name: "Bottom 2", values: [1, 2] },
        {name: "Middle", values: [3] },
        {name: "Top 2", values: [4, 5]}
        ]
    },{
        points: 6, mergings : [
        {name: "Bottom 2", values: [1, 2] },
        {name: "Middle 2", values: [3, 4] },
        {name: "Top 2", values: [5, 6]}
        ]
    },{
        points: 7, mergings: [
        {name: "Bottom 2", values: [1, 2] },
        {name: "Middle 3", values: [3, 4, 5] },
        {name: "Top 2", values: [6, 7]}
        ]
    },{
        points: 9, mergings: [
        {name: "Bottom 3", values: [1, 2, 3] },
        {name: "Middle 3", values: [4, 5, 6] },
        {name: "Top 3", values: [7, 8, 9]}
        ]
    },{
        points: 10, mergings: [
        {name: "Bottom 3", values: [1, 2, 3] },
        {name: "Middle 4", values: [4, 5, 6, 7] },
        {name: "Top 3", values: [8, 9, 10]}
        ]
    },{
        points: 11, mergings : [
        {name: "Bottom 3", values: [1, 2, 3] },
        {name: "Middle 5", values: [4, 5, 6, 7, 8] },
        {name: "Top 3", values: [9, 10, 11]}
        ]
     }
];
var points = mergings_by_points.map(function(entry) { return entry.points; });
 
 
if (!main())
	log("QScript Canceled.");
else
	conditionallyEmptyLog("QScript Finished.");
 
function main() {
    var data_file;
    if (project.dataFiles.length > 1)
        data_file = selectOneDataFile('There is more than one data file in your project. ' + 
            'Please select the data file to use:', project.dataFiles)
    else 
        data_file = project.dataFiles[0];   
 
    // Show the user an appropriate list of Pick One - Muli questions 
 
    // Get Pick One - Multi questions with appropriate number of scale points
    var pick_one_multis = data_file.questions.filter(function (q) { return !q.isHidden 
                                                                           && q.questionType == 'Pick One - Multi' 
                                                                           && points.indexOf(numberScalePoints(q)) != -1; });
 
    if (pick_one_multis.length == 0) {
        log("There are no appropriate questions in the data file.");
        return false;
    }
 
    // letting the user select the questions to flatten
    var questions_to_flatten;
    while (!questions_to_flatten || !oneOrMoreQuestions(questions_to_flatten))
        questions_to_flatten = selectManyQuestions('Please select questions to flatten:', pick_one_multis).questions;
 
    // Create new flattened questions
    var log_lines = [];
    var flattened_questions = flattenQuestions(questions_to_flatten, mergings_by_points, log_lines);
    var log_joined = log_lines.join("\r\n");
 
    // writing a report
    if (flattened_questions.length > 0) {
        var top_group_name = "Flattened and merged questions";
        var new_group = generateGroupOfSummaryTables(top_group_name, flattened_questions);
        log("Flattened and merged questions have been added to the folder: " + top_group_name + "\r\n\r\n" + log_joined);
    } else
        log("There are no questions to flatten." + "\r\n\r\n" + log_joined);

    return true;
}
 
 
// Generate a flattened version of each question
function flattenQuestions(questions_to_flatten, mergings_by_points, log_contents) {
    var flattened_questions = [];
    questions_to_flatten.forEach(function (q) {
        var flattened_name = q.name + ' (flattened)';
        var q_flattened = q.dataFile.getQuestionByName(flattened_name);
        if (q_flattened != null) {
             log_contents.push(q.name + " has already been flattened: " + flattened_name); 
        } else {
            var n_points = numberScalePoints(q);
            var k = points.indexOf(n_points);
            q_flattened = pickOneMultiToPickAnyFlattenAndMergeByRows(q, mergings_by_points[k].mergings, false);
            setCountThisValueForVariablesInQuestion(q_flattened, 1, true);
            q_flattened.needsCheckValuesToCount = false;
            flattened_questions.push(q_flattened);
        }
    });
    return flattened_questions;
}

Prior to the 15th of December, 2015, this page was known as Modifying Rows and Columns - Merge and Flatten Scales

See also