rvenn-diagram

Compare two character vectors in R


I have two character vectors of IDs.

I would like to compare the two character vectors, in particular I am interested in the following figures:

I would also love to draw a Venn diagram.


Solution

  • Here are some basics to try out:

    > A = c("Dog", "Cat", "Mouse")
    > B = c("Tiger","Lion","Cat")
    > A %in% B
    [1] FALSE  TRUE FALSE
    > intersect(A,B)
    [1] "Cat"
    > setdiff(A,B)
    [1] "Dog"   "Mouse"
    > setdiff(B,A)
    [1] "Tiger" "Lion" 
    

    Similarly, you could get counts simply as:

    > length(intersect(A,B))
    [1] 1
    > length(setdiff(A,B))
    [1] 2
    > length(setdiff(B,A))
    [1] 2