[R] Elementwise c() for lists

Thomas Lumley thomas at biostat.washington.edu
Mon Mar 20 17:43:37 CET 2000


On Mon, 20 Mar 2000, Trenkler, Dietrich wrote:

> I have two lists of equal lengths. Each one consists of vectors. Is
> there an elegant way to combine them elementwise? Elegant means: without
> loops. 
> 
> For instance, for
> 
> > jja_list(1:3,4:7)
> > jjb_list(rep(0,4),(2,5))
> 
> I would like to get:
> 
> > list(c(1:3,rep(0,4)),c(4:7,rep(2,5))).


We are currently considering a 'lapplyAll' function that would be the most
elegant solution to your problem (analogous to the LISP mapcar function).

For the time being

n<-length(list1)
sapply(1:n,function(i) c(list1[[i]],list2[[i]])

with an obvious generalisation to three or more lists.

It's not substantially faster than a for() loop, though.


Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list