[R] Basic question: Reading in multiple choice question responses to a single column in data frame

Damion Dooley damion at learningpoint.ca
Thu Aug 20 05:06:47 CEST 2009


Slight addendum.  Working from your code, I found 1 line of code does the
conversion:

	myColumn = unlist(strsplit(as.character(myData[[myQuestion]]),","));

But the dataframe you set up may prove more useful.

Regards,

Damion

-----Original Message-----
From: Magnus Torfason [mailto:zulutime.net at gmail.com] 
Sent: August 19, 2009 12:33 PM
To: Damion Dooley
Cc: r-help at r-project.org
Subject: Re: [R] Basic question: Reading in multiple choice question
responses to a single column in data frame

Are you looking for something like this?

 > d      = data.frame(a=1:5,b=c("1","2,3","2","3,4","1"))
 > d
   a   b
1 1   1
2 2 2,3
3 3   2
4 4 3,4
5 5   1
 > multis = strsplit(d$b,",")
 > counts = sapply(strsplit(d$b,","),length )  
> d2 = data.frame( a=rep(d$a,counts), b=unlist(multis) )  
> d2
   a b
1 1 1
2 2 2
3 2 3
4 3 2
5 4 3
6 4 4
7 5 1

Best,
Magnus




More information about the R-help mailing list