Last active
August 29, 2015 14:07
-
-
Save lordsutch/77c676ff2bb3a30483bb to your computer and use it in GitHub Desktop.
Turn a comma-separated list of values (like from a Google Forms spreadsheet with a checkbox form) into logical variables
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
library(stringr) | |
mga.students <- read.csv('MGA POLS 1101 Student Profile Survey (Responses) - Form Responses 1.csv') | |
## Checkbox items | |
varlist <- c('Based.on.what.you.know.off.the.top.of.your.head..without.looking.it.up...which.of.the.following.statements.about.Middle.Georgia.State.College.do.you.believe.to.be.true..You.can.mark.more.than.one.response.', | |
'Which.of.the.following.statements.reflect.s..why.you.are.planning.to.transfer...You.may.select.more.than.one.answer..', | |
'Which.of.the.following..if.any..would.make.you.more.excited.about.pursuing.your.education.at.Middle.Georgia.State.' | |
) | |
for(varname in varlist) { | |
show(varname) | |
items <- str_split(mga.students[,varname], ', ') | |
options <- unique(unlist(items)) | |
options <- options[options != ''] | |
opt.df <- t(data.frame(lapply(items, function(x) options %in% x))) | |
colnames(opt.df) <- options | |
mga.students <- cbind(mga.students, opt.df) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment