Magnus,

Looks like that solution should work, and I like the flexibility of your
data output, but I get a "error in strsplit(d$b,","): non-character
argument" at:

        multis = strsplit(d$b,",")

Seems like the c() function converts integer looking items like "1" into
integers and then strsplit fails on them?  I was running into this earlier
when attempting strsplit directly on column values.



Damion 

-----Original Message-----
From: Magnus Torfason [mailto:zulutime....@gmail.com] 
Sent: August 19, 2009 12:33 PM
To: Damion Dooley
Cc: r-help@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

______________________________________________
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.

Reply via email to