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

Damion Dooley damion at learningpoint.ca
Wed Aug 19 22:07:06 CEST 2009


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