[R] Merging nested lists

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


On Wed, 27 Oct 2010, Charles C. Berry wrote:

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

I see a few hiccups below.

Maybe Alex P. should include a minimal, self-contained example that 
RespondeRs could try out...

>
> objs <- paste('MyList',1:30,sep='')
> big.list <- lapply( objs, get )
> for (i in 1:4) big.list <- unlist(big.list,recursive=FALSE)

for (i in 1:3) ...

> dim( big.list ) <- c(30, 6, 3, 7 )

dim( big.list ) <- rev( c(30, 6, 3, 7 ) )

> res <- apply( big.list, 2:4, rbind )
> dim(res) <- c( 30, 3, 7 )

res <- apply( big.list, 1:3, function(x) list( do.call( rbind, x ) ))


Chuck


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