[R] How to get intersection of multiple vectors?

Petr Savicky savicky at cs.cas.cz
Thu Feb 2 12:16:42 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.

Set intersection is an associative operation. So,

  intersect(intersect(v1, v2), v3)

or

  intersect(v1, intersect(v2, v3))

yield the correct result.

Hope this helps.

Petr Savicky.



More information about the R-help mailing list