How to find the sum of diagonal elements in a table in R?


The sum of diagonal elements could be required in matrix analysis therefore, we can convert the matrix into a table and find the sum of diagonal elements. This can be easily done by using sun function by extracting diagonal elements of the table using diag function. For example, if we have a table T then the sum of diagonal elements of T can be found as sum(diag(T)).

Example

 Live Demo

Table1<-as.table(matrix(1:25,ncol=5))
Table1

Output

  A B  C  D  E
A 1 6 11 16 21
B 2 7 12 17 22
C 3 8 13 18 23
D 4 9 14 19 24
E 5 10 15 20 25

Example

sum(diag(Table1))

Output

[1] 65

Example

 Live Demo

Table2<-as.table(matrix(1:100,ncol=10))
Table2

Output

  A  B  C  D  E  F  G  H  I  J
A 1 11 21 31 41 51 61 71 81 91
B 2 12 22 32 42 52 62 72 82 92
C 3 13 23 33 43 53 63 73 83 93
D 4 14 24 34 44 54 64 74 84 94
E 5 15 25 35 45 55 65 75 85 95
F 6 16 26 36 46 56 66 76 86 96
G 7 17 27 37 47 57 67 77 87 97
H 8 18 28 38 48 58 68 78 88 98
I 9 19 29 39 49 59 69 79 89 99
J 10 20 30 40 50 60 70 80 90 100

Example

sum(diag(Table2))

Output

[1] 505

Example

 Live Demo

Table3<-as.table(matrix(rnorm(36),nrow=6)) Table3

Output

      A           B            C           D          E           F
A -0.02015819 -2.14686269 -0.79392704 -0.55050284  0.23070052  0.13070019
B -0.39663252 -0.12698078 -0.09832510 -1.41939702 -0.49657164 -0.45341576
C  0.23753427  0.78309823  2.11059813 -0.41943086 -0.33058117  0.63018308
D -2.03889403  1.33432969  1.65307088  1.67585600 -1.49239102 -0.67350890
E -0.61901214 -0.89328172  1.03601932 -0.16994050  0.73360113  0.42438789
F -1.09296499 -1.10922272  0.04226664  2.58299950  0.51644977  0.41344741

Example

sum(diag(Table3))

Output

[1] -1.493482

Example

 Live Demo

Table4<-as.table(matrix(rnorm(36,100,5),nrow=6))
Table4

Output

     A         B         C        D         E       F
A 105.44635  93.14600  97.80468 100.68027  95.28400 100.82330
B 95.16300   98.87825  98.75319  96.53916 108.81398  99.40972
C 94.99926  105.25513 100.37713 100.96798  93.41062 101.70070
D 99.21070   99.82776  88.46770  97.40873 102.29429  95.97573
E 98.70941   95.46398 101.49608 102.96491 101.35786 105.05309
F 102.20267 101.30244 100.53210  91.06927  87.33858 102.15255

Example

sum(diag(Table4))

Output

[1] 608.7968

Example

 Live Demo

Table5<-as.table(matrix(rnorm(25,500,50),nrow=5))
Table5

Output

    A        B        C        D        E
A 505.2863 493.6967 542.0539 577.7998 504.0781
B 500.1169 518.8777 403.9920 569.6609 506.1925
C 410.2091 404.1374 521.1845 547.0921 489.7272
D 520.4017 491.4741 502.0402 453.0907 490.6733
E 499.4468 520.5062 449.8988 541.2709 562.4680

Example

sum(diag(Table5))

Output

[1] 2422.402

Example

 Live Demo

Table6<-as.table(matrix(runif(25,5,10),nrow=5))
Table6

Output

      A       B        C         D       E
A 9.957061 8.584646 6.731691 6.645764 6.343259
B 7.157706 5.733703 5.630403 9.290109 5.232770
C 8.244165 5.308932 5.100177 6.389525 7.758126
D 6.445069 8.942210 5.995070 6.302655 8.955960
E 8.200180 7.202910 9.770459 6.822972 6.435597

Example

sum(diag(Table6))

Output

[1] 31.74213

Example

 Live Demo

Table7<-as.table(matrix(rexp(25,3.5),nrow=5)) Table7

Output

     A             B         C         D         E 
A 0.29068590 0.27290414 0.55115684 0.23493220 0.51366603
B 0.69828775 0.39694271 0.08617531 0.01405418 0.29315770
C 0.07375495 0.14626855 0.36778766 0.58536517 0.38151674
D 0.02949406 0.01493486 0.23719988 0.01521633 0.03468193
E 0.08120215 0.42675242 0.33896103 0.34181323 0.06136357

Example

sum(diag(Table7))

Output

[1] 0.310424

Example

 Live Demo

Table10<-as.table(matrix(rpois(100,15),ncol=10))
Table10

Output

   A   B    C   D  E    F   G H  I  J
A  22  21  16  13  10   17 10  6 11 13
B  9   13  17  15  18   8  12 17 12 12
C  22  24  17  14  15   17 12 19 18 18
D  15  15  20  19  12   17 13 14 7 16
E  18  10  15  15  6    15 13 27 22 11
F  9   14  11  8   15   10 19 12 17 14
G  17  14  13  12  11   12 14 16 12 18
H  15  18  13  18  18   20 13 15 6 20
I  16  16  15  14  12   15  9 17 14 14
J  13  20  21  13  18   17 11 16 15 15

Example

sum(diag(Table10))

Output

[1] 156

Updated on: 19-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements