计算矩阵中的零个数

时间:2015-10-11 14:44:33

标签: arrays r

考虑以下矩阵:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
 [1,]    2    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0
 [2,]    2    2    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0
 [3,]    2    4    2    2    2    2    2    2    4     8     6     6     4     4     6     4     4     2     2     2
 [4,]    2    2    2    4    4    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0
 [5,]    4    4    4    4    6    6    6    6    6     2     2     0     0     0     0     0     0     0     0     0
 [6,]    4    4    4    2    2    2    4    6    6     2     2     2     2     2     2     2     2     2     0     0
 [7,]    4    2    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0
 [8,]    4    6    4    6    4    6    4    4    6     2     2     0     0     0     0     0     0     0     0     0
 [9,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0
[10,]    4    6    4    4    6    8   14   10   14     8    10     8     8     6     6     4     0     0     0     0
[11,]    2    2    2    2    2    2    0    0    0     0     0     0     0     0     0     0     0     0     0     0
[12,]    2    2    4    4    2    2    2    2    0     0     0     0     0     0     0     0     0     0     0     0
[13,]    2    2    2    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0
[14,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0
[15,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0
[16,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0
[17,]    2    4    6    4    2    2    0    0    0     0     0     0     0     0     0     0     0     0     0     0
[18,]    2    4    4    4    6    4    8   10    8    14    16    14    20    24    26    16    16    20    20    22
[19,]    2    4    2    2    4    4    4    2    4     2     2     2     2     2     2     2     0     0     0     0
[20,]    2    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     0 

如何从每列中提取0的总数?

1 个答案:

答案 0 :(得分:3)

colSums(trial == 0)应用于矩阵可以很好地总结每列中的0总数