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]]
?? <lm_mengxin <at> 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
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.
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.