【问题标题】:Using combn function to create tables and label each table based on its combination使用 combn 函数创建表并根据其组合标记每个表
【发布时间】:2020-10-05 19:28:21
【问题描述】:

我有一些看起来像这样的数据:

# 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

我想为每个列的组合制作一个表格。我可以这样做:

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(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() 函数并获取每个组合名称以使表格信息更丰富?

数据:

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

【问题讨论】:

    标签: r combn


    【解决方案1】:

    不妨试试

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 2020-12-23
      • 2021-11-26
      • 2014-10-13
      • 2019-01-30
      • 1970-01-01
      • 2019-04-17
      相关资源
      最近更新 更多