如何从逻辑输入中获取信息

时间:2019-05-19 02:18:28

标签: r venn-diagram

我有这样的数据

df<- structure(list(rowid = 1:12, P = c(TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE), T = c(TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE), 
    X = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, 
    FALSE, TRUE, TRUE)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

我想查看每列之间有多少重叠,然后制作维恩图。所以我的愿望输出就是这样

        overlap      total     
P_vs_T  9            10        
P_vs_X  10           10 
T_vs_X  9            11
T_Vs_P  9            11

2 个答案:

答案 0 :(得分:2)

do.call(rbind, combn(x = names(df)[-1],
                     m = 2,
                     simplify = FALSE,
                     FUN = function(nm)
                         data.frame(overlap = rep(sum(rowSums(df[nm]) == 2), 2),
                                    total = c(sum(df[nm][,1] == 1),
                                              sum(df[nm][,2] == 1)),
                                    row.names = c(paste(nm, collapse = " vs "),
                                                  paste(rev(nm), collapse = " vs ")))))
#       overlap total
#P vs T       9    10
#T vs P       9    11
#P vs X      10    10
#X vs P      10    10
#T vs X       9    11
#X vs T       9    10

答案 1 :(得分:1)

这里是tidyverse的一种方式-

expand.grid(var1 = names(df)[-1], var2 = names(df)[-1], stringsAsFactors = F) %>% 
  filter(var1 != var2) %>% 
  mutate(
    overlap = apply(., 1, function(x) {
      sum(df[[x[1]]] & df[[x[2]]])
    }),
    total = apply(., 1, function(x) {
      sum(df[[x[1]]])
    })
  ) %>% 
  unite(var1, var2, col = "test", sep = "_vs_")

    test overlap total
1 T_vs_P       9    11
2 X_vs_P      10    10
3 P_vs_T       9    10
4 X_vs_T       9    10
5 P_vs_X      10    10
6 T_vs_X       9    11