How to find common elements from multiple vectors?
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
.
intersect_all <- function(a,b,...){ all_data <- c(a,b,...) require(plyr) count_data<- length(list(a,b,...)) freq_dist <- count(all_data) intersect_data <- freq_dist[which(freq_dist$freq==count_data),"x"] intersect_data}intersect_all(a,b,c)
UPDATE EDITA simpler code
intersect_all <- function(a,b,...){ Reduce(intersect, list(a,b,...))}intersect_all(a,b,c)