How to get intersection of multiple vectors?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get intersection of multiple vectors?

Ken Knoblauch
孟欣 <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

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get intersection of multiple vectors?

Petr Savicky
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to get intersection of multiple vectors?

Petr Savicky
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Loading...