[R] A question about "union"

Greg Snow Greg.Snow at imail.org
Fri Oct 17 18:57:36 CEST 2008


And once you have all your variables of interest in a list, you can avoid the explicit loop by using the Reduce function (which works for cases where you have a function that takes 2 arguments and you want to apply it recursively to more than 2):

> tmp <- list( a=1:10, b=5:15, c=20 )
> Reduce( union, tmp )
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 20
>

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Wacek Kusnierczyk
> Sent: Friday, October 17, 2008 5:46 AM
> To: Leon Yee
> Cc: R help
> Subject: Re: [R] A question about "union"
>
> Leon Yee wrote:
> > But the problem is that I have MANY vectors, whose names are of some
> > specific pattern. It is not good for us to write a very long
> expression.
> >
> > vectors <- list(ls(pattern="xyz"))  didn't get what I want.
>
> then you could mix the two approaches:
>
> vectors = lapply(ls(pattern="xyz"), get)
>
> u = NULL
> for (v in vectors) ...
>
> which version is better will of course depend on the particular context
> of your problem.
>
> vQ
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list