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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Agustin Lobo <alobo at ija.csic.es> writes:> I'm now using a for loop and applying > union() to each pair of vectors, but is > there a faster way avoiding the for ?Not faster, I think. Maybe neater, using something like lapply(seq(along=l1), function(i)union(l1[[i]],l2[[i]])) or (with napply from an earlier post of mine) napply(l1,l2,FUN=union) where napply <- function(..., FUN) { x <- list(...) lens <- sapply(x,length) len <- max(lens) if (any(lens != len)) x <- lapply(x, rep, length=len) tuples <- lapply(seq(length=len), function(i)lapply(x,"[", i)) lapply(tuples, function(t)eval(as.call(c(FUN,t)))) } -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
This would require either a binary form of lapply, or a version of
lapply that passed the current index to the function. Another solution
is to use an iterator closure:
f <- function(l1, l2) {
i <- 0
function(...) {
i <<- i + 1
union(l1[[i]], l2[[i]])
}
}
out <- lapply(lista1, f(lista1, lista2))
I assume here that the lists are of the same length and in the same
order.
T.
On Wed, 2002-04-17 at 09: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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Would this work?
lista <- list(1:3,4:7,2:10)
listb <- list(2:4,6:10,1:6)
lapply(1:3,function(x,lista,listb)
union(lista[[x]],listb[[x]]),lista=lista,listb=listb)
Or, alternatively, if the list has names:
lista <-
list("s"=1:3,"u"=4:7,"n"=2:10,"d"=0,"a"=-1:1,"r"=3:12)
listb <-
list("s"=2:4,"u"=6:10,"n"=1:6,"r"=2:4)
lapply(union(names(lista),names(listb)),
function(x,lista,listb)
union(lista[[x]],listb[[x]]),
lista=lista,listb=listb)
The latter option allows for lista and listb to have different lengths
as well.
Sundar
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
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
--
Sundar Dorai-Raj, Ph.D.
Statistical Methods Engineer
PDF Solutions, Inc.
(972) 889-3085 x216
(214) 392-7619 cell
sundard at pdf.com
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._