Aggregate rows

aggregateRows(x, ...)

# S4 method for matrix
aggregateRows(x, by, fun = c("sum", "mean", "median", "geometricMean", "n"))

# S4 method for Matrix
aggregateRows(x, by, fun = c("sum", "mean"))

# S4 method for SummarizedExperiment
aggregateRows(x, col = "aggregate", fun = "sum")

Arguments

x

Object.

by

factor. Aggregation groupings. The new aggregate names are defined as the factor levels, and the original, unaggregated names are defined as the names.

fun

character(1). Name of the aggregation function to apply. Uses match.arg() internally.

col

character(1). Name of column in either rowData() or colData() that defines the desired aggregation groupings.

...

Additional arguments.

Value

Modified object of same class, with rows aggregated.

Methods (by class)

  • matrix, Matrix: Aggregate using a grouping factor.

  • SummarizedExperiment: Aggregate data slotted in assays() using an automatically generated grouping factor, which is obtained from a user-defined column (col argument) in either the rowData() or colData() of the object. Slot an aggregate column into rowData() for aggregateRows(), or into colData() for aggregateCols(). This method will define the groupings automatically, and perform the aggregation.

  • SingleCellExperiment: Aggregate assays() across cell-level groupings, defined by a column in colData(). Inherits from SummarizedExperiment, and still relies upon slotting an aggregate column into colData(). Note that these groupings will map to cells, so care must be taken to properly aggregate samples.

Author

Michael Steinbaugh, Rory Kirchner

Examples

counts <- matrix( data = c( 0L, 2L, 2L, 2L, 2L, 0L, 2L, 2L, 2L, 2L, 0L, 2L, 2L, 2L, 2L, 0L ), nrow = 4L, ncol = 4L, byrow = TRUE, dimnames = list( paste0("transcript", seq_len(4L)), paste( paste0("sample", rep(seq_len(2L), each = 2L)), paste0("replicate", rep(seq_len(2L), times = 2L)), sep = "_" ) ) ) genes <- factor(paste0("gene", rep(seq_len(2L), each = 2L))) names(genes) <- rownames(counts) print(genes)
#> transcript1 transcript2 transcript3 transcript4 #> gene1 gene1 gene2 gene2 #> Levels: gene1 gene2
## matrix ==== print(counts)
#> sample1_replicate1 sample1_replicate2 sample2_replicate1 #> transcript1 0 2 2 #> transcript2 2 0 2 #> transcript3 2 2 0 #> transcript4 2 2 2 #> sample2_replicate2 #> transcript1 2 #> transcript2 2 #> transcript3 2 #> transcript4 0
aggregateRows(counts, by = genes)
#> sample1_replicate1 sample1_replicate2 sample2_replicate1 #> gene1 2 2 4 #> gene2 4 4 2 #> sample2_replicate2 #> gene1 4 #> gene2 2
## Matrix ==== sparse <- as(counts, "sparseMatrix") print(sparse)
#> 4 x 4 sparse Matrix of class "dgCMatrix" #> sample1_replicate1 sample1_replicate2 sample2_replicate1 #> transcript1 . 2 2 #> transcript2 2 . 2 #> transcript3 2 2 . #> transcript4 2 2 2 #> sample2_replicate2 #> transcript1 2 #> transcript2 2 #> transcript3 2 #> transcript4 .
aggregateRows(sparse, by = genes)
#> 2 x 4 sparse Matrix of class "dgCMatrix" #> sample1_replicate1 sample1_replicate2 sample2_replicate1 #> gene1 2 2 4 #> gene2 4 4 2 #> sample2_replicate2 #> gene1 4 #> gene2 2
## SummarizedExperiment ==== se <- SummarizedExperiment::SummarizedExperiment( assays = SimpleList(counts = counts), rowData = DataFrame(aggregate = genes) ) print(se)
#> class: SummarizedExperiment #> dim: 4 4 #> metadata(0): #> assays(1): counts #> rownames(4): transcript1 transcript2 transcript3 transcript4 #> rowData names(1): aggregate #> colnames(4): sample1_replicate1 sample1_replicate2 sample2_replicate1 #> sample2_replicate2 #> colData names(0):
aggregateRows(se)
#> class: SummarizedExperiment #> dim: 2 4 #> metadata(1): aggregate #> assays(1): counts #> rownames(2): gene1 gene2 #> rowData names(0): #> colnames(4): sample1_replicate1 sample1_replicate2 sample2_replicate1 #> sample2_replicate2 #> colData names(0):