[R] combining two list objects

Liaw, Andy andy_liaw at merck.com
Fri Dec 9 17:59:10 CET 2005


Something like this should do:

> mapply(c, list1, list2, SIMPLIFY=FALSE)
[[1]]
[[1]]$model
[1] 1

[[1]]$pcorrect
[1] 2

[[1]][[3]]
[1] 9


[[2]]
[[2]]$model
[1] 3

[[2]]$pcorrect
[1] 4

[[2]][[3]]
[1] 10

Andy


From: Rajarshi Guha
> 
> Hi, I have 2 list objects, say list1 and list2
> 
> Each element of list1 is a list with components: model, pcorrect
> Each element of list2 has a single unnamed numeric value
> 
> What I would like to do is to be able to combine list1 and 
> list2 to give
> list3 such that 
> list3 is a list where each element is a list with components: 
> 
> model, pcorrect, tcorrect
> 
> where tcorrect is a value taken from list2.
> 
> I am currently doing something like below. Is it possible to do this
> without explicitly looping?
> 
>  ll1 <- list(model=1, pcorrect=2)
>  ll2 <- list(model=3, pcorrect=4)
> 
>  list1 <- list(ll1, ll2)
>  list2 <- list(9,10)
> 
>  list3 <- list()
>  for (i in 1:length(list1)) {
>     tmp1 <- list1[[i]]
>     tmp2 <- list2[[i]]
>     list3[[i]] <- list(model=tmp1$model, pcorrect=tmp1$pcorrect,
> tcorrect=tmp2)
>  }
> 
>  > list3
> [[1]]
> [[1]]$model
> [1] 1
> 
> [[1]]$pcorrect
> [1] 2
> 
> [[1]]$tcorrect
> [1] 9
> 
> 
> [[2]]
> [[2]]$model
> [1] 3
> 
> [[2]]$pcorrect
> [1] 4
> 
> [[2]]$tcorrect
> [1] 10
>     
> Thanks,
> 
> -------------------------------------------------------------------
> Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net>
> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
> -------------------------------------------------------------------
> "whois awk?", sed Grep.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list