Re: [R] How to store and manipulate survey data like this?
On 08/13/2013 12:17 PM, Walter Anderson wrote: I have to process a set of survey data with questions that are formatted like this; 1) Pick your top three breeds (pick 3) 1 Rottweiler 2 Pit Bull 3 German Shepard 4 Poodle 5 Border Collie 6 Dalmation 7 Mixed Breed and the answers are formatted like this: Respondent, Question1 1, 1,4,7 2, 2,7,5 3, 6,3,5 4, ... Any suggestions on how to preprocess the file to be able to do things like frequency analysis for breeds? Here's how I would get started: survey - read.csv(survey.csv, as.is=TRUE) survey Respondent Question1 1 1 1,4,7 2 2 2,7,5 3 3 6,3,5 4 4 TipleOrNAs - function(x) {if (length(x) == 3) x else c(NA, NA, NA)} options - lapply(strsplit(survey$Question1, ,), TripleOrNAs) options - matrix(unlist(options), ncol=3, byrow=TRUE) survey2 - cbind(survey, options) names(survey2) - c(names(survey), paste(Q1.Opt, 1:3, sep=.)) survey2 Respondent Question1 Q1.Opt.1 Q1.Opt.2 Q1.Opt.3 1 1 1,4,7147 2 2 2,7,5275 3 3 6,3,5635 4 4 NA NA NA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to store and manipulate survey data like this?
On 08/13/2013 11:41 AM, Siraaj Khandkar wrote: On 08/13/2013 12:17 PM, Walter Anderson wrote: I have to process a set of survey data with questions that are formatted like this; 1) Pick your top three breeds (pick 3) 1 Rottweiler 2 Pit Bull 3 German Shepard 4 Poodle 5 Border Collie 6 Dalmation 7 Mixed Breed and the answers are formatted like this: Respondent, Question1 1, 1,4,7 2, 2,7,5 3, 6,3,5 4, ... Any suggestions on how to preprocess the file to be able to do things like frequency analysis for breeds? Here's how I would get started: survey - read.csv(survey.csv, as.is=TRUE) survey Respondent Question1 1 1 1,4,7 2 2 2,7,5 3 3 6,3,5 4 4 TipleOrNAs - function(x) {if (length(x) == 3) x else c(NA, NA, NA)} options - lapply(strsplit(survey$Question1, ,), TripleOrNAs) options - matrix(unlist(options), ncol=3, byrow=TRUE) survey2 - cbind(survey, options) names(survey2) - c(names(survey), paste(Q1.Opt, 1:3, sep=.)) survey2 Respondent Question1 Q1.Opt.1 Q1.Opt.2 Q1.Opt.3 1 1 1,4,7147 2 2 2,7,5275 3 3 6,3,5635 4 4 NA NA NA Thank you! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to store and manipulate survey data like this?
Hi, You could try: dat2- read.table(text=' Respondent, Question1 1, 1,4,7 2, 2,7,5 3, 6,3,5 4, ',sep=,,header=TRUE,stringsAsFactors=FALSE) library(stringr) dat2New-cbind(dat2,do.call(rbind,lapply( str_split(str_trim(dat2[,2]),,),as.numeric))) colnames(dat2New)[3:5]- paste(Q1,colnames(dat2New)[3:5],sep=.) dat2New # Respondent Question1 Q1.1 Q1.2 Q1.3 #1 1 1,4,7 1 4 7 #2 2 2,7,5 2 7 5 #3 3 6,3,5 6 3 5 #4 4 NA NA NA A.K. - Original Message - From: Walter Anderson wandrso...@gmail.com To: r-help@r-project.org Cc: Sent: Tuesday, August 13, 2013 12:17 PM Subject: [R] How to store and manipulate survey data like this? I have to process a set of survey data with questions that are formatted like this; 1) Pick your top three breeds (pick 3) 1 Rottweiler 2 Pit Bull 3 German Shepard 4 Poodle 5 Border Collie 6 Dalmation 7 Mixed Breed and the answers are formatted like this: Respondent, Question1 1, 1,4,7 2, 2,7,5 3, 6,3,5 4, ... Any suggestions on how to preprocess the file to be able to do things like frequency analysis for breeds? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.