Tables - Lots of Crosstabs

From Q
Jump to: navigation, search
Related Videos
Part 4 of Creating Tables in Q (Video)
 

This QScript generates crosstabs multiple crosstabs at the same time (also, see Tables - Banner Tables). This is an alternative to using the Basic Tables feature, which automatically flattens any two-dimensional (grid) questions.

Technical detail

You will be asked to select:

  1. The questions use in the rows of the tables (these will be placed in the Blue Drop-down Menu).
  2. The questions use in the columns of the tables (these will be placed in the Brown Drop-down Menu).

Any Pick One - Multi, Pick Any - Grid and Number - Grid questions will be copied and flattened before using them in a crosstab. The flattened copy will contain all of the cells from the original question in a single column or row.

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 to Generate Outputs");
includeWeb('QScript Selection Functions');
includeWeb('QScript Table Functions');
includeWeb('QScript Utility Functions');
 
if (!main())
    log("QScript cancelled.");
else 
    conditionallyEmptyLog("QScript finished.");
 
function main() {
 
    if (!requireDataFile()) {
        return false;
    }
 
    var column_q_choices = [];
    var row_q_choices = [];
 
    if (project.dataFiles.length > 1) 
        var data_file = selectOneDataFile('There is more than one data file in your project. ' + 
                'Please select the data file to use:', project.dataFiles)
    else
        var data_file = project.dataFiles[0];   
 
 
 
    var row_q_choices = data_file.questions.filter(function (q) { return q.isValid && !q.isHidden && q.questionType.indexOf("Text") == -1});
    var column_q_choices = row_q_choices.filter(function (q) { return q.questionType != 'Ranking' && q.questionType != 'Experiment'});
 
    if (row_q_choices.length == 0 || column_q_choices.length == 0) {
        log("Not enough questions available to make crosstabs.")
        return false;
    }
 
 
    var row_questions, column_questions;
    while (!row_questions || !oneOrMoreQuestions(row_questions))
        row_questions = selectManyQuestions('Please select questions to place in the rows:', row_q_choices).questions;
 
    while (!column_questions || !oneOrMoreQuestions(column_questions))
        column_questions = selectManyQuestions('Please select questions to place in the columns:', column_q_choices).questions;
 
 
    flattenSelectedQuestions(column_questions);
    flattenSelectedQuestions(row_questions);
 
    // check for large crosstabs
    var q_pairs_large = [];
    for (var i = 0; i < row_questions.length; i++) {
        for (var j = 0; j < column_questions.length; j++) {
            var row_q = row_questions[i];
            var column_q = column_questions[j];
            if (row_q.isValid && column_q.isValid && questionsYieldLargeCrosstab(row_q, column_q)) {
                q_pairs_large.push('"' + row_q.name + ' and ' + column_q.name + '"');
            }
        }
    }
 
    // warn about large crosstabs
    if (q_pairs_large.length == 1) {
        var cancelled = !confirm('The question pair ' + q_pairs_large[0] +
                                 ' will result in a very large table.\n\n' +
                                 'Very large tables are not likely to be interesting, will be slow to calculate ' +
                                 'and will generate a warning message when viewed.\n\n' +
                                 'Do you wish to continue?');
        if (cancelled)
            return false;
    } else if (q_pairs_large.length > 1) {
        var q_pairs_large_examples = '';
        for (var i = 0; i < Math.min(3, q_pairs_large.length); i++) {
            q_pairs_large_examples = q_pairs_large_examples + q_pairs_large[i] + '\n';
        }
        var cancelled = !confirm('There are ' + q_pairs_large.length + ' very large tables that will be created.\n\n' +
                                 'Very large tables are not likely to be interesting, will be slow to calculate ' +
                                 'and will generate a warning message when viewed.\n\n ' +
                                 'Question pairs that will result in very large tables include:\n' + q_pairs_large_examples + '\n' +
                                 'Do you wish to continue?');
        if (cancelled)
            return false;
    }
 
    var crosstabs = project.report.appendGroup();
    crosstabs.name = 'Crosstabs';
 
    for (var i = 0; i < row_questions.length; i++) {
        for (var j = 0; j < column_questions.length; j++) {
            var table = crosstabs.appendTable();
            table.primary = row_questions[i];
            table.secondary = column_questions[j];
            if (!addStat(table, 'Row %') * !addStat(table, 'n')) // non short-circuiting &&
                if (!addStat(table, 'Column n'))
                    addStat(table, 'Row n');
        }
    }
    if (fileFormatVersion() > 8.65)
        project.report.setSelectedRaw([crosstabs.subItems[0]]);
    conditionallyEmptyLog('Crosstabs have been added to the "' + crosstabs.name + '" group.');
    return true;
} 
 
 
function addStat(table, stat) {
    cell_stats = table.cellStatistics;
    cell_stats.push(stat);
    table.cellStatistics = cell_stats;
    return table.cellStatistics.indexOf(stat) > -1;
}
 
function oneOrMoreQuestions(questions) {
    if (questions.length == 0) {
        alert('Select one or more questions.');
        return false;
    } else
        return true;
}


See also

Personal tools
Namespaces

Variants
Actions
Navigation
Categories
Toolbox