rvectorset-operations

How to find elements common in at least 2 vectors?


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

Solution

  • It is much simpler than a lot of people are making it look. This should be very efficient.

    1. Put everything into a vector:

      x <- unlist(list(a, b, c, d, e))
      
    2. Look for duplicates

      unique(x[duplicated(x)])
      # [1] 2 3 1 4 8
      

    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 with x <- 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:

    which(tabulate(x) >= n)
    

    if the data is only made of natural integers (1, 2, etc.) as in the example. If not:

    f <- table(x)
    names(f)[f >= n]
    

    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.