Daniel Brewer
2009-Apr-06 14:38 UTC
[R] Collapse data matrix with extra info separated by commas
Hello, I would like to reshape my data for presentation purposes from something like this:> test <-data.frame(a=c("A","A","A","A","B","B","B"),b=c(1,1,2,2,1,1,1),c=1:7)> testa b c 1 A 1 1 2 A 1 2 3 A 2 3 4 A 2 4 5 B 1 5 6 B 1 6 7 B 1 7 to something like this: a b c 1 A 1 1,2 3 A 2 3,4 5 B 1 5,6,7 This seems to be the sort of the thing that the reshape library should be able to do, but I just can't work out how to do it. Many thanks Dan -- ************************************************************** Daniel Brewer, Ph.D. Institute of Cancer Research Molecular Carcinogenesis Email: daniel.brewer at icr.ac.uk ************************************************************** The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP. This e-mail message is confidential and for use by the a...{{dropped:2}}
baptiste auguie
2009-Apr-06 15:40 UTC
[R] 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>> test <- > data.frame(a=c("A","A","A","A","B","B","B"),b=c(1,1,2,2,1,1,1),c=1:7)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:> Hello, > > I would like to reshape my data for presentation purposes from > something > like this: >> test <- > data.frame(a=c("A","A","A","A","B","B","B"),b=c(1,1,2,2,1,1,1),c=1:7) >> test > a b c > 1 A 1 1 > 2 A 1 2 > 3 A 2 3 > 4 A 2 4 > 5 B 1 5 > 6 B 1 6 > 7 B 1 7 > > to something like this: > a b c > 1 A 1 1,2 > 3 A 2 3,4 > 5 B 1 5,6,7 > > This seems to be the sort of the thing that the reshape library should > be able to do, but I just can't work out how to do it. > > Many thanks > > Dan > > -- > ************************************************************** > Daniel Brewer, Ph.D. > > Institute of Cancer Research > Molecular Carcinogenesis > Email: daniel.brewer at icr.ac.uk > ************************************************************** > > The Institute of Cancer Research: Royal Cancer Hospital, a > charitable Company Limited by Guarantee, Registered in England under > Company No. 534147 with its Registered Office at 123 Old Brompton > Road, London SW7 3RP. > > This e-mail message is confidential and for use by the...{{dropped:26}}
jim holtman
2009-Apr-06 16:01 UTC
[R] Collapse data matrix with extra info separated by commas
try this:> x <- lapply(split(test, list(test$a, test$b), drop=TRUE), function(.data){+ data.frame(a=.data$a[1], b=.data$b[1], c=paste(.data$c, collapse=',')) + })> do.call(rbind, x)a b c A.1 A 1 1,2 B.1 B 1 5,6,7 A.2 A 2 3,4>On Mon, Apr 6, 2009 at 10:38 AM, Daniel Brewer <daniel.brewer at icr.ac.uk> wrote:> Hello, > > I would like to reshape my data for presentation purposes from something > like this: >> test <- > data.frame(a=c("A","A","A","A","B","B","B"),b=c(1,1,2,2,1,1,1),c=1:7) >> test > ?a b c > 1 A 1 1 > 2 A 1 2 > 3 A 2 3 > 4 A 2 4 > 5 B 1 5 > 6 B 1 6 > 7 B 1 7 > > to something like this: > ?a b c > 1 A 1 1,2 > 3 A 2 3,4 > 5 B 1 5,6,7 > > This seems to be the sort of the thing that the reshape library should > be able to do, but I just can't work out how to do it. > > Many thanks > > Dan > > -- > ************************************************************** > Daniel Brewer, Ph.D. > > Institute of Cancer Research > Molecular Carcinogenesis > Email: daniel.brewer at icr.ac.uk > ************************************************************** > > The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP. > > This e-mail message is confidential and for use by the...{{dropped:19}}