Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (19k points)

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?

1 Answer

0 votes
by (33.1k points)
edited by

You should simply perform 

sum(diag(tbl))

here tbl is your table.

Hope this answer helps!

If you want to explore more in R programming then watch this R programming tutorial for beginner:

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...