Intersect in R

A colleague asked me this question: "I'm trying to find a way to find genes that overlap between three datasets. I have used intersect for two dataframes but can't seem to find a solution for three dataframes on google. Do you know any snazzy way of doing that?" I thought using the venn() function from the gplots package is a pretty snazzy solution, so that's what I recommended.

The venn() function is used to plot a Venn diagrams for up to 5 sets but you can use it to list intersections.

# install if necessary
install.packages('gplots')
library(gplots)

a <- 1:10
b <- 3:12
c <- 5:14
d <- 7:16
e <- 9:18

my_list <- list(a, b, c, d, e)
venn(my_list)

# save object from venn
my_venn <- venn(my_list, show.plot = FALSE)
class(my_venn)
[1] "venn"

# which object class?
library(pryr)
otype(my_venn)
[1] "S3"

# what are the attributes names?
names(attributes(my_venn))
[1] "dim"           "dimnames"      "intersections" "class"

# use attr() to get intersections
attr(x = my_venn, "intersections")
$A
[1] "1" "2"

$E
[1] "17" "18"

$`A:B`
[1] "3" "4"

$`D:E`
[1] "15" "16"

$`A:B:C`
[1] "5" "6"

$`C:D:E`
[1] "13" "14"

$`A:B:C:D`
[1] "7" "8"

$`B:C:D:E`
[1] "11" "12"

$`A:B:C:D:E`
[1] "9"  "10"

# get specific intersections
attr(x = my_venn, "intersections")$`A:B`
[1] "3" "4"

A bit hard to visualise the intersections when there are five lists.

Notice that attr(x = my_venn, "intersections")$A:B gives you only 3 and 4 and not 3, 4, 5, 6, 7, 8, 9, and 10? This is because 3 and 4 are the only intersections that are unique to A and B, when taking the other intersections into account.

intersect(a, b)
[1]  3  4  5  6  7  8  9 10

# an element in the union of C, D, and E may or may not intersect A and B
# however, if they do intersect any element in A and B, then that element is not
# unique to intersect(a, b)
setdiff(intersect(a, b), union(union(c, d), e))
[1] 3 4

# and finally the non-snazzy way of intersecting three lists
intersect(a, intersect(b,c))
[1]  5  6  7  8  9 10
Print Friendly



Creative Commons License
This work is licensed under a Creative Commons
Attribution 4.0 International License
.
4 comments Add yours

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.