我有下表:
>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
> 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" ...
> 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")
我想计算每行的总和,对角线比例值的百分比:
(421/(421+409+31+10 + 1 + 3 + 1)) + (113 / (100 + 113 + 17 + 12 + 2))
是否有更有效的方法来计算它而不是使用rowSums(a)
并使用循环计算对角线值除以每行的总和?