Skip to content

Instantly share code, notes, and snippets.

@lordsutch
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lordsutch/77c676ff2bb3a30483bb to your computer and use it in GitHub Desktop.
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
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