R Language Set operations Measuring set overlaps / Venn diagrams for vectors


Example

To count how many elements of two sets overlap, one could write a custom function:

xtab_set <- function(A, B){
    both    <-  union(A, B)
    inA     <-  both %in% A
    inB     <-  both %in% B
    return(table(inA, inB))
}

A = 1:20
B = 10:30

xtab_set(A, B)

#        inB
# inA     FALSE TRUE
#   FALSE     0   10
#   TRUE      9   11

A Venn diagram, offered by various packages, can be used to visualize overlap counts across multiple sets.