sum, colSums, rowSums

sum, colSums, rowSums – calc. sums
使用例: sum(OBJ,na.rm=T/F)
概要:
sum OBJ全体の和を計算(欠損値を除外して計算する場合はna.rm=Tとする)
colSums 列毎にOBJの和を計算(e.g. 変数毎の和)
rowSums 行毎にOBJの和を計算(e.g. 被験者毎の和)
関連: var, sd, mean

# usage #
sum(OBJ)
colSums(OBJ)
rowSums(OBJ)

##### ----- examples ----- #####
> datMat<-matrix(rnorm(10),ncol=2)
> datMat
            [,1]        [,2]
[1,] -0.52014830 -1.09940927
[2,] -0.17118129 -1.75202117
[3,]  1.87727940 -0.06483163
[4,] -0.09374804 -1.19832566
[5,]  0.91919805  0.74479903
> sum(datMat) #全体の和
[1] -1.358389
> colSums(datMat) #列毎の和(e.g. 変数毎の和)
[1]  2.011400 -3.369789
> rowSums(datMat) #行毎の和(e.g. 被験者毎の和)
[1] -1.619558 -1.923202  1.812448 -1.292074  1.663997
 
# 欠損値がある場合の例
> datMat[1,1]=NaN
> datMat
            [,1]        [,2]
[1,]         NaN -1.09940927
[2,] -0.17118129 -1.75202117
[3,]  1.87727940 -0.06483163
[4,] -0.09374804 -1.19832566
[5,]  0.91919805  0.74479903
> sum(datMat)
[1] NaN
> sum(datMat,na.rm=T)
[1] -0.8382406
> colSums(datMat)
[1]       NaN -3.369789
> colSums(datMat,na.rm=T)
[1]  2.531548 -3.369789
> rowSums(datMat)
[1]       NaN -1.923202  1.812448 -1.292074  1.663997
> rowSums(datMat,na.rm=T)
[1] -1.099409 -1.923202  1.812448 -1.292074  1.663997
##### ----- end examples ----- #####

Leave a Reply