rvectorr-faq

How to find common elements from multiple vectors?


Can anyone tell me how to find the common elements from multiple vectors?

a <- c(1,3,5,7,9)
b <- c(3,6,8,9,10)
c <- c(2,3,4,5,7,9)

I want to get the common elements from the above vectors (ex: 3 and 9)


Solution

  • There might be a cleverer way to go about this, but

    intersect(intersect(a,b),c)
    

    will do the job.

    EDIT: More cleverly, and more conveniently if you have a lot of arguments:

    Reduce(intersect, list(a,b,c))