使用 combn 函数创建 table 并根据其组合标记每个 table

Using combn function to create tables and label each table based on its combination

我有一些数据看起来像:

# A tibble: 10 x 7
   XGBoost   Logistic  NN        RandomForest SVMRadial SVMLinear LightGBM 
   <chr>     <chr>     <chr>     <chr>        <chr>     <chr>     <chr>    
 1 Correct   Correct   Correct   Correct      Correct   Correct   Correct  
 2 Correct   Correct   Correct   Correct      Correct   Correct   Correct

我想对每个列的组合进行 table。我可以这样做:

combn(d, 2, function(x){
  table(x[[1]],
        x[[2]])
})

这给了我类似的东西:

, , 1

     [,1] [,2]
[1,]    8    0
[2,]    0    2

, , 2

     [,1] [,2]
[1,]    8    0
[2,]    0    2

, , 3

     [,1] [,2]
[1,]    8    0
[2,]    0    2

但是,我无法分辨这些组合来自 table 中的哪一个。我可以单独做类似的事情:

table(d$XGBoost, d$Logistic) %>% 
  matrix(
    nrow = 2,
    dimnames = list("XGBoost" = c("Correct", "Incorrect"),
                    "Logistic" = c("Correct", "Incorrect"))
  )

给出:

           Logistic
XGBoost     Correct Incorrect
  Correct         8         0
  Incorrect       0         2

哪个信息量更大。

如何应用 combn() 函数并获取每个组合名称,以便 table 提供更多信息?

数据:

d <- structure(list(XGBoost = c("Correct", "Correct", "Correct", "Correct", 
"Correct", "Correct", "Incorrect", "Incorrect", "Correct", "Correct"
), Logistic = c("Correct", "Correct", "Correct", "Correct", "Correct", 
"Correct", "Incorrect", "Incorrect", "Correct", "Correct"), NN = c("Correct", 
"Correct", "Correct", "Correct", "Correct", "Correct", "Incorrect", 
"Incorrect", "Correct", "Correct"), RandomForest = c("Correct", 
"Correct", "Correct", "Correct", "Correct", "Correct", "Incorrect", 
"Incorrect", "Correct", "Correct"), SVMRadial = c("Correct", 
"Correct", "Correct", "Correct", "Correct", "Correct", "Incorrect", 
"Incorrect", "Correct", "Correct"), SVMLinear = c("Correct", 
"Correct", "Correct", "Correct", "Correct", "Correct", "Incorrect", 
"Incorrect", "Correct", "Correct"), LightGBM = c("Correct", "Correct", 
"Correct", "Correct", "Correct", "Correct", "Incorrect", "Incorrect", 
"Correct", "Correct")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

也许试试

combn(d, 2, table, simplify = FALSE)