Skip to content

Instantly share code, notes, and snippets.

@ctesta01
Last active July 21, 2016 14:32
Show Gist options
  • Save ctesta01/a033f984176492a212abcfae25acdf06 to your computer and use it in GitHub Desktop.
Save ctesta01/a033f984176492a212abcfae25acdf06 to your computer and use it in GitHub Desktop.
QualtricsTools: How MCSA Questions Work

This page was last updated on July 21st, 2016.

The commit this reflects the code from is: 82af5

Multiple Choice Single Answer

Line 35, R/results_generation.R is where we begin.

mc_single_answer_results <- function(question) {

First, we determine what format the response cells will be in.

    if ("RecodeValues" %in% names(question[['Payload']]) && length(question[['Payload']][['RecodeValues']]) > 0) {
        factors <- unlist(question[['Payload']][['RecodeValues']])
    } else {
        factors <- names(question[['Payload']][['Choices']])
    }

In multiple choice single answer (mcsa) questions, there's only one response column of numeric raw responses (but there could be text entry components), so we use the fact that a mcsa question's numeric response column is always named with that question's data export tag to select the right column.

    responses <- question[['Responses']][[question[['Payload']][['DataExportTag']]]]

Next, we table the respondents by the factors, and select the column from the factor-table that includes the number of respondents for each factor.

    responses_tabled <- as.data.frame(table(factor(responses, levels=factors)))
    N <- responses_tabled[,2]

To get the denominator count, we count the number of respondents whose response is anything but a "-99" or a "" (blank).

    exporttag <- question[['Payload']][['DataExportTag']]
    respondent_count <- length(which((question[['Responses']][[exporttag]] != -99) & (question[['Responses']][[exporttag]] != "")))

Finally, we take our list of the number of respondents by factor (N), and we divide it by the total respondent_count, format it as a percentage, and we have our "Percent" column for our table.

    Percent <- percent0(N / respondent_count)

Now, we have all the information, but we need to construct the results table. If "RecodeValues" appears in the question, we use the question[['Payload']][['RecodeValues']] as a map from the numeric responses to the indices of the choices as they are indexed in the question[['Payload']][['Choices']] list.

    if ("RecodeValues" %in% names(question[['Payload']]) && length(question[['Payload']][['RecodeValues']]) > 0) {
        choices_recoded <- responses_tabled[,1]
        choices_uncoded <- sapply(choices_recoded, function(x) which(question[['Payload']][['RecodeValues']] == x))
        choices <- sapply(choices_uncoded, function(x) question[['Payload']][['Choices']][[x]][[1]])
    } else {

Otherwise, we just directly use the question[['Payload']][['Choices']] list.

    } else {
        choices_uncoded <- responses_tabled[,1]
        choices <- sapply(choices_uncoded, function(x) question[['Payload']][['Choices']][[x]][[1]])
    }

After we've gotten the choices, we make sure to clean them up and make sure that they are formatted as an atomic vector (meaning that there isn't depth to the list, every element is on the top).

    choices <- unlist(choices, use.names = FALSE)
    choices <- sapply(choices, clean_html)

Finally, we construct the data table, give the choices column a "" for the title, and return the table.

    results_table <- data.frame(N, Percent, choices, row.names = NULL)
    colnames(results_table)[3] <- ""
    results_table
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment