Say I have 5 vectors:
a <- c(1,2,3)
b <- c(2,3,4)
c <- c(1,2,5,8)
d <- c(2,3,4,6)
e <- c(2,7,8,9)
I know I can calculate the intersection between all of them by using Reduce()
together with intersect()
, like this:
Reduce(intersect, list(a, b, c, d, e))
[1] 2
But how can I find elements that are common in, say, at least 2 vectors? i.e.:
[1] 1 2 3 4 8
You could try all possible combinations, for example:
This is an approach that counts the number of vectors each unique value occurs in.
Yet another approach, applying a vectorised function with
outer
:The output above gives the number of pairs of vectors that share each of the duplicated elements 1, 2, 3, 4, and 8.
A variation of @rengis method would be:
where,
It is much simpler than a lot of people are making it look. This should be very efficient.
Put everything into a vector:
Look for duplicates
and
sort
if needed.Note: In case there can be duplicates within a list element (which your example does not seem to implicate), then replace
x
withx <- unlist(lapply(list(a, b, c, d, e), unique))
Edit: as the OP has expressed interest in a more general solution where n >= 2, I would do:
if the data is only made of natural integers (1, 2, etc.) as in the example. If not:
This is now not too far from James solution but it avoids the costly-ish
sort
. And it is miles faster than computing all possible combinations.Here's another option: