DelayedMatrix-rowsum: rowsum() and colsum() on a DelayedMatrix object

DelayedMatrix-rowsumR Documentation

rowsum() and colsum() on a DelayedMatrix object

Description

Like ordinary matrices in base R, DelayedMatrix objects and derivatives support rowsum() and colsum().

Details

Note that the rowsum() and colsum() operations are not delayed: the output matrix is realized block by block. The automatic realization backend controls where realization happens e.g. in memory as an ordinary matrix if not set (i.e. set to NULL), or in an HDF5 file if set to "HDF5Array". See ?setAutoRealizationBackend for more information about realization backends.

Value

The object returned by the rowsum() or colsum() method for DelayedMatrix objects will be either:

  • An ordinary matrix if the automatic realization backend is NULL (the default).

  • A DelayedMatrix object if the automatic realization backend is not NULL. In this case, the returned DelayedMatrix object will be either pristine or made of several pristine DelayedMatrix objects bound together (via rbind() or cbind(), both are delayed operations).

    For example, if the automatic realization backend is "HDF5Array", then the returned DelayedMatrix object will be either an HDF5Array object, or it will be a DelayedMatrix object made of several HDF5Array objects bound together.

See Also

  • rowsum in base R.

  • S4Arrays::rowsum in the S4Arrays package for the rowsum() and colsum() S4 generic functions.

  • getAutoRealizationBackend and setAutoRealizationBackend for getting and setting the automatic realization backend.

  • matrixStats-methods for DelayedMatrix row/col summarization.

  • DelayedMatrix-mult for DelayedMatrix multiplication and cross-product.

  • DelayedArray objects.

  • writeHDF5Array in the HDF5Array package for writing an array-like object to an HDF5 file and other low-level utilities to control the location of automatically created HDF5 datasets.

  • HDF5Array objects in the HDF5Array package.

Examples

library(HDF5Array)
set.seed(123)
m0 <- matrix(runif(14400000), ncol=2250,
             dimnames=list(NULL, sprintf("C%04d", 1:2250)))
M0 <- writeHDF5Array(m0, chunkdim=c(200, 250))
dimnames(M0) <- dimnames(m0)

## --- rowsum() ---

group <- sample(90, nrow(M0), replace=TRUE)  # define groups of rows
rs <- rowsum(M0, group)
rs[1:5, 1:8]
rs2 <- rowsum(M0, group, reorder=FALSE)
rs2[1:5, 1:8]

## Let's see block processing in action:
DelayedArray:::set_verbose_block_processing(TRUE)
setAutoBlockSize(2e6)
rs3 <- rowsum(M0, group)
setAutoBlockSize()
DelayedArray:::set_verbose_block_processing(FALSE)

## Sanity checks:
stopifnot(all.equal(rowsum(m0, group), rs))
stopifnot(all.equal(rowsum(m0, group, reorder=FALSE), rs2))
stopifnot(all.equal(rs, rs3))

## --- colsum() ---

group <- sample(30, ncol(M0), replace=TRUE)  # define groups of cols
cs <- colsum(M0, group)
cs[1:5, 1:7]
cs2 <- colsum(M0, group, reorder=FALSE)
cs2[1:5, 1:7]

## Sanity checks:
stopifnot(all.equal(colsum(m0, group), cs))
stopifnot(all.equal(cs, t(rowsum(t(m0), group))))
stopifnot(all.equal(cs, t(rowsum(t(M0), group))))
stopifnot(all.equal(colsum(m0, group, reorder=FALSE), cs2))
stopifnot(all.equal(cs2, t(rowsum(t(m0), group, reorder=FALSE))))
stopifnot(all.equal(cs2, t(rowsum(t(M0), group, reorder=FALSE))))

Bioconductor/DelayedArray documentation built on Dec. 7, 2024, 10:27 p.m.