# [R] How to union the elements in a list?

Bert Gunter gunter.berton at gene.com
Wed Oct 28 20:51:11 CET 2009

```... and just for amusement: unique(do.call(c,l))

The do.call and unlist approaches should be faster than Reduce; do.call
_may_ be marginally faster than unlist. Here's a timing comparison:

> z <- split(sample(1000,1e6,rep=TRUE),rep(1:1e5,10))
> length(z)
[1] 100000

## the comparisons:

> system.time(y1 <- Reduce(union,z))
user  system elapsed
5.02    0.00    5.03

> system.time(y2 <- unique(unlist(z)))
user  system elapsed
1.92    0.00    1.92

> system.time(y3 <- unique(do.call(c,z)))
user  system elapsed
1.75    0.00    1.75

> identical(y1,y2)
[1] TRUE
> identical(y2,y3)
[1] TRUE

Obviously, this is unlikely to matter for any reasonable size dataset, but
maybe it's instructive.

Of course, Reduce wins the RGolf contest  ;-)

Bert Gunter
Genentech Nonclinical Biostatistics

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Ben Bolker
Sent: Wednesday, October 28, 2009 12:27 PM
To: r-help at r-project.org
Subject: Re: [R] How to union the elements in a list?

Peng Yu wrote:
>
> Suppose that I have a list of vectors. I want to compute the union of
> all the vectors in the list. I could use 'for' loop to do so. But I'm
> wondering what would be a better solution that does not need a 'for'
> loop.
>
> l=list(a=c(1,3,4), b=c(1,3,6), c=c(1,3,7), ....)
>
>

Reduce(union,l)

--
View this message in context:
http://www.nabble.com/How-to-union-the-elements-in-a-list--tp26100375p261006
84.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help