How to get counts of intersections of six or more sets?

后端 未结 3 1881
谎友^
谎友^ 2021-01-28 02:09

I am running an analysis of a number of sets and I have been using the package VennDiagram, which has been working just fine, but it only handles up to 5 sets, and now it turns

3条回答
  •  闹比i
    闹比i (楼主)
    2021-01-28 02:51

    OK, here's one way, assuming you represent sets as a list of vectors, and items to be searched in those sets also as vector:

    # Example data format
    sets <- list(v1 = 1:6, v2 = 1:8, v3 = 3:8)
    items <- c(2:7)
    
    # Search for items in each set
    result <- data.frame(searched = items)
    for (set in names(sets)) {
      result <- cbind(result, items %in% sets[[set]])
      names(result)[length(names(result))] <- set
    }
    
    # Count
    library(plyr)
    ddply(result, names(sets), function (i) {
      data.frame(count = nrow(i))
    })
    

    This gives you all combinations actually existing in the itemset:

         v1   v2    v3 count
    1 FALSE TRUE  TRUE     1
    2  TRUE TRUE FALSE     1
    3  TRUE TRUE  TRUE     4
    

提交回复
热议问题