[R] Merging nested lists

Charles C. Berry cberry at tajo.ucsd.edu
Thu Oct 28 05:40:31 CEST 2010


On Wed, 27 Oct 2010, Alex P. wrote:

> Hello All,
>
> I have multiple "list of lists" in the form of
>
> Mylist1[[N]][[K]]$Name_i,
>
> with N=1..6, K=1..3, and i=1..7. Each Name_i is a matrix. I have 30 of these objects Mylist1, Mylist2, ...
>
> I would like to merge these lists by each Name_i using rbind, but I 
> couldn't figure out how to do it. What I want at the end is a single 
> "list of lists", again in the form of Mylist[[N]][[K]]$Name_i. Manually 
> doing it is not feasible given the large number of Mylist objects.
>

Turn them into a single array of mode 'list', then do the rbind'ing, and 
save the result as an array (or see ?relist for imposing the nested 
structure of the orignal lists)

Something like:

objs <- paste('MyList',1:30,sep='')
big.list <- lapply( objs, get )
for (i in 1:4) big.list <- unlist(big.list,recursive=FALSE)
dim( big.list ) <- c(30, 6, 3, 7 )
res <- apply( big.list, 2:4, rbind )
dim(res) <- c( 30, 3, 7 )


HTH,

Chuck

> Thanks in advance,
>
> Alex
>
> ______________________________________________
> R-help at 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list