How to find common elements from multiple vectors?

后端 未结 3 533
挽巷
挽巷 2020-11-22 13:04

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)

3条回答
  •  清酒与你
    2020-11-22 13:40

    A good answer already, but there are a couple of other ways to do this:

    unique(c[c%in%a[a%in%b]])
    

    or,

    tst <- c(unique(a),unique(b),unique(c))
    tst <- tst[duplicated(tst)]
    tst[duplicated(tst)]
    

    You can obviously omit the unique calls if you know that there are no repeated values within a, b or c.

提交回复
热议问题