R

A fix for Qualtrics random selection data

Sometimes I get the problems no-one else has a solution for, particular those involving data transformation.

Qualtrics (online survey tool) lets you create random selection questions, where you provide a pool of answer options and set Qualtrics to randomly display a specified number of the answer options to the person doing the survey. Which can be very useful to people who want that.

When the data is downloaded, each option is coded for them choosing the option or not (not choosing it is coded 0) with the information on which answer options they saw in a seperate display order column that gives the numbers of the options they saw separated by vertical bars (example non-real survey).

screen-shot-2017-02-18-at-1-25-19-pm

As each question option has three states:

seen and ticked, seen and not ticked, and not seen so not ticked

but the column is coded for two states

ticked, and not ticked

It is much easier to analyse if the data is reorganised. This R solution leverages two key things

  • that the top row variable names in the csv follow a predictable pattern
  • that you can refer to pieces of data by row number and variable name

In this case, it is replacing the “0” with “unticked” for those entries that were seen but not ticked.

# Section 1, Read in the qualtrics csv

qualt_file <- file.choose()
qualt_data <- read.csv(qualt_file, stringsAsFactors = FALSE)
# The double layer of headings renders everything as text
# but that is a good thing so it is all the same data type

#####################################
# Section 2, fix the question
# This section could be repeated multiple times for different questions
# just change the line starting fix_me to the appropriate question.

fix_me <- "Q2"
viewed_questions <- qualt_data[,paste("DO.Q.",fix_me,sep="")]

for (i in 2:length(viewed_questions)){
 offered_options <- unlist(strsplit(viewed_questions[i], split="|", fixed=TRUE))
 for (each_selection in offered_options){
 column_of_interest <- paste(fix_me, each_selection, sep="_")
 if(qualt_data[i,column_of_interest] == "0"){
 qualt_data[i,column_of_interest] <- "unticked"
 }
 }
}

#####################################
# Final Section, save a new version with "_new" at the end of the name
new_file <- paste(gsub(".csv", "", qualt_file), "new.csv", sep="_")
write.csv(qualt_data, file=new_file, row.names=FALSE)
# only side effect is that headings with a -, like DO-Q-Q2, now have ., like DO.Q.Q2
# as - in variable names is not legal in R.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s