title: Calculating the diagonal proportion (total Accuracy) of table in R
body: <p>I have the following table:</p>
  
  <pre><code>&gt;a
  
  scores   0   1   2   3   4   5   6   7   8   9
       0 421 409  31  10   1   3   1   0   0   0
       1 100 113  17  12   2   0   0   0   0   0
  &gt; str (a)
   'table' int [1:2, 1:10] 421 100 409 113 31 17 10 12 1 2 ...
   - attr(*, "dimnames")=List of 2
    ..$ scores: chr [1:2] "0" "1"
    ..$       : chr [1:10] "0" "1" "2" "3" ...
  
  &gt; dput(a)
  structure(c(421L, 409L, 309L, 113L, 31L, 17L, 10L, 12L, 1L, 2L, 
  3L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), .Dim = c(2L, 10L), .Dimnames = structure(list(
      scores = c("0", "1"), c("0", "1", "2", "3", "4", "5", "6", 
      "7", "8", "9")), .Names = c("scores", "")), class = "table")
  </code></pre>
  
  <p>I would like to calculate the sum for each row the percentage of the diagonal proportion value:</p>
  
  <pre><code>(421/(421+409+31+10 + 1  + 3 +  1)) + (113 / (100 + 113 + 17 + 12 + 2))
  </code></pre>
  
  <p>Is there a more efficient way to calculate it than using <code>rowSums(a)</code> and using loop for the calculating the diagonal value divided by the sum of each row?</p>