Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Function that assists with importing CSV data from Qualtrics into R; it also allows for choosing which categorical variables should be numeric and which labelled.
import_csv_qualtrics <- function(file) {
# import a csv file exported from Qualtrics
# Qualtrics places variable labels in the second row.
# This function removes that second row and returns the data frame.
# taken from:
DF <- read.csv(file, skip=2, header=FALSE)
DF2 <- read.csv(file)
names(DF) <- names(DF2)
import_qualtrics <- function(label_csv_file, numeric_csv_file, numeric_fields = NULL,
dputnames = TRUE, export_all = TRUE) {
# label_csv_file: csv file exported from Qualtrics with labels
# numeric_csv_file: csv file exported from Qualtrics with numbers instead of labels
# numeric_fields: variable names for categorical questions where you want to import
# the numeric response
# dputnames: By default, the function will print all variable names
# This is useful. You can then copy and paste the variables
# into a vector to include in the "numeric_fields" variable.
# export_all: By default, the function will export label, numeric, and combined data.frames
# If FALSE, then it will just export the combined data.frame.
ldf <- import_csv_qualtrics(label_csv_file)
ndf <- import_csv_qualtrics(numeric_csv_file)
if (dputnames) cat(dput(names(ldf)))
original_order <- names(ldf)
cdf <- data.frame(ldf[setdiff(original_order, numeric_fields)], ndf[numeric_fields])
cdf <- cdf[original_order]
if (export_all) {
list(combined = cdf, label = ldf, numeric = ndf)
} else {
# Step 1:
# Export CSV data from Qualtrics twice (once with numbers for categories and once with labels)
# and give files clear names
# Step2: Run it once with
# rcases <- import_qualtrics("raw-data/label-myfile.csv", "raw-data/numeric-myfile.csv")
# identify which variables should be numeric.
# The dput output makes it easier to select the numeric variables.
# Step 3: Re-run the command including the vector of variable names that are meant to
# be numeric (e.g., likert survey items, and so on)
# rcases <- import_qualtrics("raw-data/label-myfile.csv", "raw-data/numeric-myfile.csv",
# numeric_fields = c("item1", "item2"),
# dput = FALSE, export_all = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.