Intersection of lists in R

gappy picture gappy · Jul 8, 2011 · Viewed 14.2k times · Source

Is there a function that receives a list x and returns a list y such that y[[i]] = intersect(x[[1]][[i]], x[[2]][[i]], ...) ?

If not, is there a R way to code it in a couple of lines?

Answer

pengchy picture pengchy · Dec 20, 2016

It seems the Reduce can be simply used as follows:

> Reduce(intersect,  list(v1 = c("a","b","c","d"), 
+                         v2 = c("a","b","e"), 
+                         v3 = c("a","f","g"))) 
[1] "a"