rowsum() methods for sparse matrices
rowsum-methods.RdThe SparseArray package provides memory-efficient
rowsum() and colsum()
methods for SparseMatrix and dsparseMatrix derivatives.
Usage
# S4 method for class 'SparseMatrix'
rowsum(x, group, reorder=TRUE, ...)
# S4 method for class 'dsparseMatrix'
rowsum(x, group, reorder=TRUE, ...)
# S4 method for class 'SparseMatrix'
colsum(x, group, reorder=TRUE, ...)
# S4 method for class 'dsparseMatrix'
colsum(x, group, reorder=TRUE, ...)Arguments
- x
A SparseMatrix derivative (e.g. SVT_SparseMatrix or COO_SparseMatrix object), or dsparseMatrix derivative (e.g. dgCMatrix or dgTMatrix object).
- group, reorder
See
?base::rowsumfor a description of these arguments.- ...
Like the default S3
rowsum()method defined in the base package, the methods documented in this man page support additional argumentna.rm, set toFALSEby default. IfTRUE, missing values (NAorNaN) are omitted from the calculations.
Value
An ordinary matrix, like the default rowsum() method.
See ?base::rowsum for how the matrix returned
by the default rowsum() method is obtained.
See also
rowsumin base R.S4Arrays::rowsumin the S4Arrays package for therowsum()andcolsum()S4 generic functions.SparseMatrix objects.
dgCMatrix-class in the Matrix package.
Examples
svt0 <- randomSparseMatrix(7e5, 100, density=0.15)
dgcm0 <- as(svt0, "dgCMatrix")
m0 <- as.matrix(svt0)
group <- sample(10, nrow(m0), replace=TRUE)
## Calling rowsum() on the sparse representations is usually faster
## than on the dense representation:
rs1 <- rowsum(m0, group)
rs2 <- rowsum(svt0, group) # about 3x faster
rs3 <- rowsum(dgcm0, group) # also about 3x faster
## Sanity checks:
stopifnot(identical(rs1, rs2), identical(rs1, rs3))