Produces the same result as apply(x, 1, sd)
or apply(x, 2, sd)
without coercing matrix to dense matrix. Values are not identical to
sd()
because of the floating point precision issue in C++.
Arguments
- x
a base::matrix or Matrix::Matrix object.
Examples
mt <- Matrix::rsparsematrix(100, 100, 0.01)
colSds(mt)
#> [1] 0.19549874 0.21032176 0.03700000 0.00000000 0.13000000 0.06400000
#> [7] 0.00000000 0.29910390 0.00000000 0.00000000 0.11000000 0.00000000
#> [13] 0.19997704 0.02800000 0.34000000 0.20148487 0.07900000 0.00000000
#> [19] 0.19850716 0.07009378 0.07612430 0.00000000 0.19000000 0.00000000
#> [25] 0.06500000 0.00000000 0.23810012 0.20709389 0.00000000 0.16202955
#> [31] 0.02400000 0.00000000 0.00000000 0.23804761 0.02400000 0.00000000
#> [37] 0.08300000 0.00000000 0.12000000 0.16000000 0.00000000 0.18474892
#> [43] 0.21243181 0.06200000 0.02600000 0.21111658 0.11235064 0.27000000
#> [49] 0.00000000 0.04600000 0.37056183 0.00000000 0.00000000 0.09900000
#> [55] 0.16000000 0.21191908 0.03025998 0.00000000 0.00000000 0.00000000
#> [61] 0.00000000 0.12000000 0.23963567 0.03500000 0.00000000 0.07200000
#> [67] 0.12031524 0.16000000 0.03300000 0.21256253 0.19000000 0.13028248
#> [73] 0.00000000 0.08102194 0.00000000 0.00000000 0.18675799 0.00000000
#> [79] 0.05500000 0.15000000 0.00000000 0.16963645 0.00000000 0.05000000
#> [85] 0.00000000 0.00000000 0.00000000 0.09400000 0.03900000 0.04000000
#> [91] 0.12352610 0.03057430 0.04000000 0.00000000 0.00000000 0.00000000
#> [97] 0.00000000 0.02700000 0.09200000 0.08100000
apply(mt, 2, sd) # the same
#> [1] 0.19549874 0.21032176 0.03700000 0.00000000 0.13000000 0.06400000
#> [7] 0.00000000 0.29910390 0.00000000 0.00000000 0.11000000 0.00000000
#> [13] 0.19997704 0.02800000 0.34000000 0.20148487 0.07900000 0.00000000
#> [19] 0.19850716 0.07009378 0.07612430 0.00000000 0.19000000 0.00000000
#> [25] 0.06500000 0.00000000 0.23810012 0.20709389 0.00000000 0.16202955
#> [31] 0.02400000 0.00000000 0.00000000 0.23804761 0.02400000 0.00000000
#> [37] 0.08300000 0.00000000 0.12000000 0.16000000 0.00000000 0.18474892
#> [43] 0.21243181 0.06200000 0.02600000 0.21111658 0.11235064 0.27000000
#> [49] 0.00000000 0.04600000 0.37056183 0.00000000 0.00000000 0.09900000
#> [55] 0.16000000 0.21191908 0.03025998 0.00000000 0.00000000 0.00000000
#> [61] 0.00000000 0.12000000 0.23963567 0.03500000 0.00000000 0.07200000
#> [67] 0.12031524 0.16000000 0.03300000 0.21256253 0.19000000 0.13028248
#> [73] 0.00000000 0.08102194 0.00000000 0.00000000 0.18675799 0.00000000
#> [79] 0.05500000 0.15000000 0.00000000 0.16963645 0.00000000 0.05000000
#> [85] 0.00000000 0.00000000 0.00000000 0.09400000 0.03900000 0.04000000
#> [91] 0.12352610 0.03057430 0.04000000 0.00000000 0.00000000 0.00000000
#> [97] 0.00000000 0.02700000 0.09200000 0.08100000