rcombn

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


I have some data which looks like:

# 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

I want to make a table of each of the combinations of the columns. I can do something like:

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

Which gives me something like:

, , 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

However, I cannot tell which of the tables the combinations come from. I can individually do something like:

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

Which gives:

           Logistic
XGBoost     Correct Incorrect
  Correct         8         0
  Incorrect       0         2

Which is more informative.

How can I apply the combn() function and get each of the combination names such that the tables are more informative?

Data:

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"))

Solution

  • Perhaps try

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