colsum {DelayedMatrixStats}R Documentation

Give Column and Row Sums of a Matrix-Like Object Based on a Grouping Variable

Description

Compute column and row sums across rows or columns of a numeric matrix-like object for each level of a grouping variable.

NOTE: This man page is for the colsum() and rowsum() S4 generic functions defined in the DelayedMatrixStats package. See base::rowsum() for the default method of rowsum() (defined in the base) package. The colsum() generic is the natural extension of rowsum() but has no equivalent in the base package. Bioconductor packages can define specific methods for objects (typically matrix-like) not supported by the default method.

Usage

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

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

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

Arguments

x

A matrix-like object. Missing values are allowed. A numeric vector will be treated as a column vector.

group

A vector or factor giving the grouping, with one element per row of x for rowsum() or one element per column of x for colsum(). Missing values will be treated as another group and a warning will be given.

reorder

If TRUE, then the result will be in order of sort(unique(group)). If FALSE, it will be in the order that groups are encountered.

...

Additional arguments passed to specific methods.

na.rm

logical (TRUE or FALSE). Should NA (including NaN) values be discarded?

Details

The default is for rowsum() (resp. colsum()) to reorder the rows (columns) to agree with base::tapply() as in the example below. Reordering should not add noticeably to the time except when there are very many distinct values of group and x has few columns (rows).

To sum over all the rows (columns) of a matrix (i.e. a single group) use colSums() (rowSums()), which should be even faster. To sum over a subset of rows and/or columns of a matrix (i.e. a subset of a single group) use colSums2() (rowSums2()).

Value

A matrix-like object containing the sums. For rowsum(), there will be one row per unique value of group. For colsum(), there will be one column per unique value of group.

See base::rowsum() for the value returned by the default rowsum() method.

Specific methods defined in Bioconductor packages will typically return an object of the same class as the input object.

See Also

Examples

rowsum
showMethods("rowsum")
selectMethod("rowsum", "ANY") # the default method


[Package DelayedMatrixStats version 1.4.0 Index]