Skip to content
Prev 176265 / 398503 Next

Collapse data matrix with extra info separated by commas

Here's one attempt with plyr, hopefully Hadley will give you a better  
solution ( I could not get cast() to do it either)

test <-  
data 
.frame 
(a=c("A","A","A","A","B","B","B"),b=c(1,1,2,2,1,1,1),c=sample(1:7))
ddply(test,.(a,b),.fun=function(.) paste(.)[3])

   a b         V1
1 A 1    c(2, 4)
2 B 1 c(7, 1, 6)
3 A 2    c(3, 5)

# note that with your example R seems to use some magic
a b  V1
1 A 1 1:2
2 B 1 5:7
3 A 2 3:4

I have no idea how this happens!


HTH,

baptiste
On 6 Apr 2009, at 15:38, Daniel Brewer wrote: