sparseMatrixStats

Hexsticker

The sparseMatrixStats package reimplements the matrixStats API for dgCMatrix objects.

You can install the package from Bioconductor since version 3.11 (May 2020):

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

BiocManager::install("sparseMatrixStats")

Example

Load package

library(sparseMatrixStats)

Create a large sparse matrix

set.seed(1)
big_mat <- matrix(0, nrow=1e4, ncol=6)
big_mat[sample(seq_len(1e4 * 6), 5000)] <- rnorm(5000)
# Convert dense matrix to sparse matrix
big_sparse_mat <- as(big_mat, "dgCMatrix")
big_sparse_mat[1:5, ]
## 5 x 6 sparse Matrix of class "dgCMatrix"
##                        
## [1,] . . .        . . .
## [2,] . . .        . . .
## [3,] . . .        . . .
## [4,] . . 0.905848 . . .
## [5,] . . .        . . .
dim(big_sparse_mat)
## [1] 10000     6

Run all kinds of summary functions:

# colSums2(big_mat)
colVars(big_sparse_mat)
## [1] 0.08294950 0.09325314 0.08044417 0.08734151 0.09504466 0.08791036
colMedians(big_sparse_mat)
## [1] 0 0 0 0 0 0
colAnyNAs(big_sparse_mat)
## [1] FALSE FALSE FALSE FALSE FALSE FALSE
colRanges(big_sparse_mat)
##           [,1]     [,2]
## [1,] -2.441299 3.055742
## [2,] -3.208057 3.153971
## [3,] -3.008049 2.714331
## [4,] -3.671300 3.639574
## [5,] -2.996949 3.064524
## [6,] -3.539586 3.810277
colQuantiles(big_sparse_mat)
##             0% 25% 50% 75%     100%
## [1,] -2.441299   0   0   0 3.055742
## [2,] -3.208057   0   0   0 3.153971
## [3,] -3.008049   0   0   0 2.714331
## [4,] -3.671300   0   0   0 3.639574
## [5,] -2.996949   0   0   0 3.064524
## [6,] -3.539586   0   0   0 3.810277
colMadDiffs(big_sparse_mat)
## [1] 0 0 0 0 0 0
# ...