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++.
Examples
mt <- Matrix::rsparsematrix(100, 100, 0.01)
colSds(mt)
#> [1] 0.03885989 0.28565714 0.20000000 0.00000000 0.03500000 0.01200000
#> [7] 0.00000000 0.13009875 0.00000000 0.00000000 0.04400000 0.00000000
#> [13] 0.15869655 0.00820000 0.13000000 0.22131608 0.08000000 0.00000000
#> [19] 0.19353271 0.05973502 0.17177890 0.00000000 0.03900000 0.00000000
#> [25] 0.07700000 0.00000000 0.11909168 0.16099125 0.00000000 0.10017011
#> [31] 0.11000000 0.00000000 0.00000000 0.11336167 0.05200000 0.00000000
#> [37] 0.15000000 0.00000000 0.19000000 0.09400000 0.00000000 0.09120578
#> [43] 0.11734848 0.02600000 0.03100000 0.11870445 0.15280208 0.00000000
#> [49] 0.00000000 0.04500000 0.11055416 0.00000000 0.00380000 0.11000000
#> [55] 0.13000000 0.23180887 0.05717906 0.00000000 0.00000000 0.00000000
#> [61] 0.00000000 0.10000000 0.13231673 0.00220000 0.00000000 0.08900000
#> [67] 0.30920393 0.04000000 0.20000000 0.14093838 0.04500000 0.08422499
#> [73] 0.00000000 0.09600000 0.00000000 0.00000000 0.09671493 0.00000000
#> [79] 0.03600000 0.01900000 0.00000000 0.11430952 0.00000000 0.13000000
#> [85] 0.00000000 0.00000000 0.00000000 0.09400000 0.08600000 0.02100000
#> [91] 0.18007848 0.22575295 0.14000000 0.00000000 0.00000000 0.00000000
#> [97] 0.00000000 0.07700000 0.08300000 0.11000000
apply(mt, 2, sd) # the same
#> [1] 0.03885989 0.28565714 0.20000000 0.00000000 0.03500000 0.01200000
#> [7] 0.00000000 0.13009875 0.00000000 0.00000000 0.04400000 0.00000000
#> [13] 0.15869655 0.00820000 0.13000000 0.22131608 0.08000000 0.00000000
#> [19] 0.19353271 0.05973502 0.17177890 0.00000000 0.03900000 0.00000000
#> [25] 0.07700000 0.00000000 0.11909168 0.16099125 0.00000000 0.10017011
#> [31] 0.11000000 0.00000000 0.00000000 0.11336167 0.05200000 0.00000000
#> [37] 0.15000000 0.00000000 0.19000000 0.09400000 0.00000000 0.09120578
#> [43] 0.11734848 0.02600000 0.03100000 0.11870445 0.15280208 0.00000000
#> [49] 0.00000000 0.04500000 0.11055416 0.00000000 0.00380000 0.11000000
#> [55] 0.13000000 0.23180887 0.05717906 0.00000000 0.00000000 0.00000000
#> [61] 0.00000000 0.10000000 0.13231673 0.00220000 0.00000000 0.08900000
#> [67] 0.30920393 0.04000000 0.20000000 0.14093838 0.04500000 0.08422499
#> [73] 0.00000000 0.09600000 0.00000000 0.00000000 0.09671493 0.00000000
#> [79] 0.03600000 0.01900000 0.00000000 0.11430952 0.00000000 0.13000000
#> [85] 0.00000000 0.00000000 0.00000000 0.09400000 0.08600000 0.02100000
#> [91] 0.18007848 0.22575295 0.14000000 0.00000000 0.00000000 0.00000000
#> [97] 0.00000000 0.07700000 0.08300000 0.11000000