# How to get intersection of multiple vectors?

 Classic List Threaded
4 messages
Reply | Threaded
Open this post in threaded view
|

## How to get intersection of multiple vectors?

 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. Many thanks         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: How to get intersection of multiple vectors?

 孟欣 163.com> writes: > > 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. > Reduce(intersect,  list(v1 = c("a","b","c","d"),               v2 = c("a","b","e"),               v3 = c("a","f","g"))) > Many thanks -- Ken Knoblauch Inserm U846 Stem-cell and Brain Research Institute Department of Integrative Neurosciences 18 avenue du Doyen Lépine 69500 Bron France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: +33 (0)6 84 10 64 10 http://www.sbri.fr/members/kenneth-knoblauch.html______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: How to get intersection of multiple vectors?

 In reply to this post by 孟欣 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: How to get intersection of multiple vectors?

 In reply to this post by 孟欣 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.   ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.