[R] rbind for a list

David Winsemius dwinsemius at comcast.net
Tue Sep 29 20:05:03 CEST 2009


On Sep 29, 2009, at 1:43 PM, Carlos Hernandez wrote:

> Dear All,
> I´m using the following code:
>
> all1<-gg2[[1]][[1]]; for(i in 1:48){ all1 <- rbind(all1,gg2[[i]] 
> [[1]]) }

Looks to me that you would be getting a duplicate copy of the first  
matrix, but aside from that what problems are you experiencing that  
make you want different approaches? You have  shot your self in the  
foot for using simple methods by creating a more complex than needed  
list structure:

 > gg3 <- list(matrix(1:4, 2), matrix(5:8,2))
 > gg3
[[1]]
      [,1] [,2]
[1,]    1    3
[2,]    2    4

[[2]]
      [,1] [,2]
[1,]    5    7
[2,]    6    8

 > gg3 <- list(list(matrix(1:4, 2)), list(matrix(5:8,2)))
 > gg3
[[1]]
[[1]][[1]]
      [,1] [,2]
[1,]    1    3
[2,]    2    4


[[2]]
[[2]][[1]]
      [,1] [,2]
[1,]    5    7
[2,]    6    8

This does work,  but it is not "intuitive:

 > rbind2 <- function (x) Reduce("rbind", x)
 > rbind2(lapply(gg3, "[[", 1))
      [,1] [,2]
[1,]    1    3
[2,]    2    4
[3,]    5    7
[4,]    6    8

-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

>
> to create a new matrix that contains all the matrices in a list  
> called gg2.
> gg2 is a list that looks like
>
>>> gg2
> [[1]]
> [[1]][[1]]
> <matrix one>
>
> [[2]]
> [[2]][[1]]
> <matrix two>
> .
> .
> .
> [[48]]
> [[48]][[1]]
> <matrix 48>
>
> Is there a faster way to do the rbind?
>
> i've tried do.call("rbind",gg2) but does not work.
>
> Thank you for any hints or advice!
>
> Best regards,




More information about the R-help mailing list