0

I am looking at model comparison where I have calculated several different confusion matricies built with the ROCR package. They are currently in a form like a_cm, b_cm, c_cm.... etc

I can get the accuracy of the confusion matrix by selecting:

a_cm_accuracy <- a_cm$overall[1]

What i'd like to do is something like

confusion_matricies <- c(a_cm, b_cm, c_cm, d_cm, e_cm, f_cm, g_cm)
accuracy <- lapply(confusion_matricies, function(x) x['overall'][1])

But I know that won't work and just returns null. I tried this as well to get the first element by each variable but need to go a level down and not quite sure how:

test_apply2 <- lapply(confusion_matricies,"[",1)

If I could just get a list of the accuracy statistics that would loop through a list of these that would be great. Thanks!

Drthm1456
  • 409
  • 9
  • 17
  • It should be `function(x) x[['overall']][1]` (note the double brackets). – MrFlick Aug 09 '18 at 16:01
  • When I try to do that I get the following message:Error in x[["overall"]] : subscript out of bounds – Drthm1456 Aug 09 '18 at 16:38
  • 1
    Then make sure to provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so that we can run the code to see what's going on. Maybe you also need `confusion_matricies <- list(a_cm, ...)` (use `list` rather than `c`). You might not have the list you think you do. But that's just a guess. It's much easier to help and solve a problem with a reproducible example. – MrFlick Aug 09 '18 at 16:41
  • That worked by changing it to a list, thank you. I also tweaked what I was working towards by rewriting it: test_apply <- sapply(confusion_matricies, function(x) x[['overall']][1]). – Drthm1456 Aug 09 '18 at 16:44

0 Answers0