Creating Top and Bottom 2 or 3 Box NETs for all Questions with 5 or 7 Categories

From Q
Jump to navigation Jump to search

This code:

  • Creates top and bottom two box scores for questions with a five point scale.
  • Creates top and bottom two and three box scores for questions with seven point scales.
  • Places all the modified tables in a folder in the Report tree.

This example can be run using C:\Program Files\Q\Examples\Pasta.Q (this may be located on a different place on your computer depending upon how Q was installed). This project contains multiple 5-point scales questions but none with 7-point scales.

var new_group = project.report.appendGroup();
new_group.name = "Tables with Top and Bottom boxes";

for (var data_i in project.dataFiles) {
    var data = project.dataFiles[data_i];
    // All questions in this data file.
    for (var q_i in data.questions) {
        var q = data.questions[q_i];
        // But only non-hidden Pick One (and Multi) questions.
        if ((q.questionType == "Pick One" || q.questionType == "Pick One - Multi") && !q.isHidden) {
            // Don't consider missing values.
            var values = q.uniqueValues;
            // Search backwards through the list, because every time we remove a value,
            // the list gets smaller.
            for (var value_i = values.length - 1; value_i >= 0; value_i --) {
                if (q.valueAttributes.getIsMissingData(values[value_i]))
                    values.splice(value_i, 1);
            }
            //alert(q.name + ": " + values.length + " values");
            // Only questions with 5 or 7 non-missing values.
            if (values.length == 5 || values.length == 7) {
                // Add bottom/top two boxes.
                var bottom_label = "Bottom 2 Box";
                var top_label = "Top 2 Box";
                try {
                    q.dataReduction.createNET([q.valueAttributes.getLabel(values[0]),
                                               q.valueAttributes.getLabel(values[1])],
                                              bottom_label);
                    q.dataReduction.createNET([q.valueAttributes.getLabel(values[values.length - 2]),
                                               q.valueAttributes.getLabel(values[values.length - 1])],
                                              top_label);
                } catch (error) {
                    log("Ignored question " + q.name + " because the top 2/bottom 2 box values could not be found in its dataReduction.");
                    continue; // skip this question
                }
                q.dataReduction.moveAfter(bottom_label, null);
                q.dataReduction.moveAfter(top_label, null);
                // Add bottom/top three boxes.
                if (values.length == 7) {
                    bottom_label = "Bottom 3 Box";
                    top_label = "Top 3 Box";
                    q.dataReduction.createNET([q.valueAttributes.getLabel(values[0]),
                                               q.valueAttributes.getLabel(values[1]),
                                               q.valueAttributes.getLabel(values[2])],
                                              bottom_label);
                    q.dataReduction.createNET([q.valueAttributes.getLabel(values[values.length - 3]),
                                               q.valueAttributes.getLabel(values[values.length - 2]),
                                               q.valueAttributes.getLabel(values[values.length - 1])],
                                               top_label);
                    q.dataReduction.moveAfter(bottom_label, null);
                    q.dataReduction.moveAfter(top_label, null);
                }
                
                // Show it in a new table.
                var t = new_group.appendTable();
                t.primary = q;
            }
        }
    }
}
log("All done");


See also