Search code examples
rdataframedata-visualizationvenn-diagram

convert dataframe to venn diagram table


So, I writting a function that takes dataframe and unique number <1, 5> let say we want a unique number to be 3 in this case

   how_much = 100
   A <- sample(how_much, replace = TRUE, x = 1:5) 
   B <- sample(how_much, replace = TRUE, x = 1:5)    
   VennData <- data.frame(A, B)

and then return a described table as below:

count    A      B
 24     TRUE   TRUE
 20     TRUE   FALSE
 13     FALSE  TRUE
 43     FALSE  FALSE

when we can see that we have 24 observations where both A and B is equal to 3, 20 observations have A equal to 3 and B non equal to 3, 13 observations have A not equal to 3 and B equal to 3 etc...


Solution

  • With set.seed(43)

    library(dplyr)
    VennData %>%
        mutate(A = (A == 3),
               B = (B == 3)) %>%
        count(A, B)
    ## A tibble: 4 x 3
    #  A     B         n
    #  <lgl> <lgl> <int>
    #1 FALSE FALSE    64
    #2 FALSE TRUE     20
    #3 TRUE  FALSE    13
    #4 TRUE  TRUE      3
    

    In base R,

    aggregate(Count ~ ., transform(VennData, A = A == 3, B = B == 3, Count = 1), sum)
    #      A     B Count
    #1 FALSE FALSE    64
    #2  TRUE FALSE    13
    #3 FALSE  TRUE    20
    #4  TRUE  TRUE     3