dgCMatrix-utils {DelayedArray} | R Documentation

## Some utilities to operate on dgCMatrix objects

### Description

NOTE: These utilities are currently implemented in the DelayedArray package but they should really go somewhere else at some point (e.g. to the MatrixGenerics and sparseMatrixStats packages after they make it into Bioconductor).

### Usage

    ## rowsum() and colsum() S4 generics:

    #rowsum(x, group, reorder=TRUE, ...)
    #colsum(x, group, reorder=TRUE, ...)

    ## Default methods:

    ## S4 method for signature 'ANY'
    rowsum(x, group, reorder=TRUE, ...)
    ## S4 method for signature 'ANY'
    colsum(x, group, reorder=TRUE, ...)

    ## rowsum() method for dgCMatrix objects:

    ## S4 method for signature 'dgCMatrix'
    rowsum(x, group, reorder=TRUE, ...)

### Arguments

`x` | A numeric matrix-like object.
`group, reorder, ...` | See `?base::rowsum` for a description of these arguments.

### Value

See `?base::rowsum` for the value returned by the default `rowsum` method.

The default `colsum` method returns `t(rowsum(t(x), group, reorder=reorder, ...))`.

### See Also

- DelayedMatrix-utils in this package for the `rowsum` and `colsum` methods defined for DelayedMatrix objects.

- `base::rowsum` in the base package for the default `rowsum` method.

- The Matrix package (from CRAN) where dgCMatrix objects are defined and implemented.

### Examples

    library(Matrix)

    randomSparseMatrix <- function(nrow, ncol, sparsity=0.85)
    {
        m_len <- nrow * ncol
        m_data <- runif(m_len) * 50
        zidx <- sample(m_len, as.integer(m_len * sparsity))
        m_data[zidx] <- 0
        matrix(m_data, nrow=nrow, ncol=ncol)
    }
    m <- randomSparseMatrix(1e5, 800)  # dense representation
    m0 <- as(m, "dgCMatrix")           # sparse representation
    group <- sample(20, nrow(m), replace=TRUE)

    ## 'rowsum(m0)' is about 5x faster than 'rowsum(m)':
    rs <- rowsum(m, group)
    rs0 <- rowsum(m0, group)

    stopifnot(identical(rs, rs0))

[Package DelayedArray version 0.14.1 Index]