Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum leading diagonal of table in R

I have a table created using the table() command in R:

   y
x    0  1  2  3  4  5  6  7  8  9
  0 23  0  0  0  0  1  0  0  0  0
  1  0 23  1  0  1  0  1  2  0  2
  2  1  1 28  0  0  0  1  0  2  2
  3  0  1  0 24  0  1  0  0  0  1
  4  1  1  0  0 34  0  3  0  0  0
  5  0  0  0  0  0 33  0  0  0  0
  6  0  0  0  0  0  2 32  0  0  0
  7  0  1  0  1  0  0  0 36  0  1
  8  1  1  1  1  0  0  0  1 20  1
  9  1  3  0  1  0  1  0  1  0 24

This table shows the results of a classification, and I want to sum the leading diagonal of it (the diagonal with the large numbers - like 23, 23, 28 etc). Is there a sensible/easy way to do this in R?

like image 221
robintw Avatar asked Apr 13 '11 11:04

robintw


People also ask

How do you find the sum of the diagonals of an element?

b = trace( A ) calculates the sum of the diagonal elements of matrix A : tr ( A ) = ∑ i = 1 n a i i = a 11 + a 22 + ... + a n n .

What is the sum of diagonal?

The numbers that are diagonal to each other add up to make the same number because you're adding one that's lower or higher by 1, 2 or 3 to the number beside it. For example, in a 3x3 square, the number in the top right is 2 more than the number in the top left.

How do you find the leading diagonal matrix?

The elements of the leading diagonal of L are all ones so that . Since U is upper triangular, its determinant is the product of the elements of its leading diagonal. Thus, taking account of row interchanges the appropriately signed product of the diagonal elements of U gives the determinant.


1 Answers

How about sum(diag(tbl)), where tbl is your table?

like image 140
NPE Avatar answered Oct 08 '22 09:10

NPE