[R] union of lists

ggrothendieck@yifan.net ggrothendieck at yifan.net
Wed Apr 17 21:51:25 CEST 2002



Paste the lists together to create character strings and then parse them
back into R expressions and apply unique:

list1 <- list(c(1,34,5),c(2,1),c(3,10,15))
list2 <- list(c(1,5),c(2,1),c(3,10,29))

xx <- gsub("\\) *c\\(",",",paste(list1,list2))
xx <- sapply( xx , FUN=function(x)unique(eval(parse(text=x))) )




On 17 Apr 2002 at 15:24, Agustin Lobo wrote:

> Hi there,
> 
> Given 2 lists of integer vectors, i.e.:
> 
> > lista1
> $"1"
> [1]  1 34  5
> $"2"
> [1] 2 1
> $"3"
> [1]  3 10 15
> 
> > lista2
> $"1"
> [1] 1 5
> $"2"
> [1] 2 1
> $"3"
> [1]  3 10 29
> 
> 
> I want to obtain the union of both, defined
> as the union of the vectors, that is 
> lista.union[[1]] <- union(lista1[[1]],lista2[[1]]):
> 
> > lista.union
> $"1"
> [1]  1 34  5
> $"2"
> [1] 2 1
> $"3"
> [1]  3 10 15 29
> 
> I'm now using a for loop and applying
> union() to each pair of vectors, but is
> there a faster way avoiding the for ?
> 
> Thanks!
> 
> Agus
> 
> 
> Dr. Agustin Lobo
> Instituto de Ciencias de la Tierra (CSIC)
> Lluis Sole Sabaris s/n
> 08028 Barcelona SPAIN
> tel 34 93409 5410
> fax 34 93411 0012
> alobo at ija.csic.es
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 


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