I have a data frame which looks like this:
df<- data.frame("iteration" = c(1,1,1,1,1,1),
"model" = c("RF","RF","RF","SVM", "SVM","SVM"),
"label" = c(0,0,1,0,0,1), "prediction" = c(0,1,1,0,1,1))
iteration model label prediction
1 1 RF 0 0
2 1 RF 0 1
3 1 RF 1 1
4 1 SVM 0 0
5 1 SVM 0 1
6 1 SVM 1 1
Actually, it has 10 iterations
, more models and more data for each model.
What I am trying to do is basically to get the accuracy for each model.
So basically I want to apply this to each model group (RF,SVM):
table(df$label,df$prediction)
0 1
0 2 2
1 0 2
Them sum the diagonal and divided by the total:
sum(diag(table(df$label,df$prediction)))/sum(table(df$label,df$prediction))
[1] 0.6666667
Is this a case where I can use tapply
or is dplyr
comes in handy?
I am quite lost here.