title: Calculate accuracy by groups
body: <p>I have a data frame which looks like this:</p>
  
  <pre><code>df&lt;- 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
  </code></pre>
  
  <p>Actually, it has <code>10 iterations</code>, more models and more data for each model. </p>
  
  <p>What I am trying to do is basically to get the accuracy for each model.</p>
  
  <p>So basically I want to apply this to each model group (RF,SVM):</p>
  
  <pre><code>table(df$label,df$prediction)
  
      0 1
    0 2 2
    1 0 2
  </code></pre>
  
  <p>Them sum the diagonal and divided by the total:</p>
  
  <pre><code>sum(diag(table(df$label,df$prediction)))/sum(table(df$label,df$prediction))
  [1] 0.6666667
  </code></pre>
  
  <p>Is this a case where I can use <code>tapply</code> or is <code>dplyr</code>comes in handy?</p>
  
  <p>I am quite lost here.</p>