title: Extract metrics and populate data frame from multiple confusionMatrix's
body: <p>I have a number of confusionMatrix outputs for a variety of different classification models. I would like to extract the model metrics from each confusion matrix (i.e. Sensitivity --> Balanced Accuracy, or all metrics from cf_1$byClass) to populate a data frame. I would like the data frame to contain all these metrics, as well as a label for which confusion matrix it came from. Can anyone help me figure out how to do this? Please see below for a reproducible example:</p>
  
  <pre><code>library(caret)
  library(tidyverse)
  
  df_1 &lt;- data.frame(x = sample(LETTERS[1:2], 20, replace = T),
                        y = sample(LETTERS[1:2], 20, replace = T))
  
  df_2 &lt;- data.frame(x = sample(LETTERS[1:2], 20, replace = T),
                        y = sample(LETTERS[1:2], 20, replace = T))
  
  cf_1 &lt;- confusionMatrix(df_1$y, df_1$x)
  cf_2 &lt;- confusionMatrix(df_2$y, df_2$x)
  
  # I would like a dataframe with each of these, as well as column for the associated confusion matrix name
  cf_1$byClass
  cf_2$byClass
  </code></pre>
  
  <p>My desired output would look like this, except with all relevant metrics:</p>
  
  <pre><code>x = c(0.2, 0.4)
  y = c(0.5, 0.6)
  z = c("cf_1", "cf_2"
        )
  
  output &lt;- data.frame(Sensitivity = x, Specificity = y, Model = z)
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>This is the solution that I came up with, it's not pretty, but it works. That being said, still open to seeing if anyone can come up with a cleaner, more efficient way to do this!</p>
  
  <pre><code>model_names &lt;- c("cf_1", "cf_2")
  model_metrics &lt;- list()
  for (i in 1:length(model_names)) {
       for (j in model_names) {
            model &lt;- eval(as.name(j))
            results &lt;- model$byClass %&gt;% round(digits = 3)
            results &lt;- c(model = j, results)
            model_metrics[[j]] &lt;- results
       }
  }
  
  model_metrics &lt;- do.call(rbind, model_metrics) %&gt;% 
       data.frame %&gt;% 
       `rownames&lt;-`(NULL)
  
  model_metrics
  </code></pre>