Last active
February 23, 2017 15:09
-
-
Save jeromyanglim/8b3e55cb06628fee9776ae897fe987e9 to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
# http://lowlywonk.blogspot.com.au/2011/05/r-code-to-remove-second-line-of.html | |
DF <- read.csv(file, skip=2, header=FALSE) | |
DF2 <- read.csv(file) | |
names(DF) <- names(DF2) | |
DF | |
} | |
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 { | |
cdf | |
} | |
} | |
# 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