Skip to content

Instantly share code, notes, and snippets.

@ctesta01
Created July 21, 2016 14:44
Show Gist options
  • Save ctesta01/ef79b916dab9015cc51a22628d1412cc to your computer and use it in GitHub Desktop.
Save ctesta01/ef79b916dab9015cc51a22628d1412cc to your computer and use it in GitHub Desktop.
QualtricsTools: How MCMA Questions Work

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

The commit this reflects the code from is: 82af5

Multiple Choice Multiple Answer

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

mc_multiple_answer_results <- function(question) {

First, we need to figure out the numeric response columns. We're going to use the fact that all text entry components of a question label their response columns with "TEXT" in the name. From this, we know we can select the columns that don't have "TEXT" in the column name.

  orig_responses <- question[['Responses']]
  not_text_columns <- which(sapply(colnames(orig_responses), function(x) !(grepl("TEXT", x))))
  orig_responses <- orig_responses[, not_text_columns]

Next, for each of these columns we sum the positive respondents (those who checked the response, indicated by a "1"), and we count the total number of valid respondents (those who responded to any part of the question at all).

  N <- sapply(orig_responses, function(x) sum(x == 1))
  respondents_count <- length(which(apply(
    orig_responses, 1, function(row) !(all(row == -99) | all(row == "")))))

Now we have a list of the number of positive respondents for each question part (N), but its names for each list element are still the response column names. We clean those up a little bit by removing the data export tag (along with an underscore).

  data_export_tag <- question[['Payload']][['DataExportTag']]
  names(N) <- gsub(paste0(data_export_tag, "_"), "", names(orig_responses))

If "RecodeValues" appears in the question, we use it to translate from the values remaining as the names in the list N to the indices of the choices in question[['Payload']][['Choices']].

  if ("RecodeValues" %in% names(question[['Payload']]) && all(names(N) %in% question[['Payload']][['RecodeValues']])) {
    names(N) <- sapply(names(N), function(x) names(question[['Payload']][['RecodeValues']])[which(question[['Payload']][['RecodeValues']] == x)])
  }

Now, we replace the names in N with the text of the choices, and clean them of HTML.

  choices <- lapply(names(N), function(x) question[['Payload']][['Choices']][[x]][[1]])
  choices <- lapply(choices, clean_html)
  choices <- unlist(choices, use.names = FALSE)

Next to last, we calculate the percentages of respondents who chose each choice.

  N <- unlist(N, use.names = FALSE)
  Percent <- percent0(N / respondents_count)

And finally, we table the results and return them:

  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