[R] How to get intersection of multiple vectors?

Petr Savicky savicky at cs.cas.cz
Thu Feb 2 12:23:23 CET 2012


On Thu, Feb 02, 2012 at 01:55:07PM +0800, 孟欣 wrote:
> v1<-c("a","b","c","d")
> v2<-c("a","b","e")
> v3<-c("a","f","g")
> 
> 
> I want to get the intersection of v1,v2,v3,ie "a"
> 
> 
> How can I do then?
> 
> 
> What I know is only for 2 vectors via "intersect" function,but don't know how to deal with multiple vectors.

Hi.

Try the following

  intersectSeveral <- function(...) { Reduce(intersect, list(...)) }
  intersectSeveral(v1, v2, v3)

  [1] "a"

Hope this helps.

Petr Savicky.
 



More information about the R-help mailing list