Aggregate gene/transcript features (rows) or sample replicates (columns).

aggregateCols(object, ...)

aggregateRows(object, ...)

# S4 method for matrix
aggregateRows(object, groupings, fun = c("sum",
  "mean"))

# S4 method for sparseMatrix
aggregateRows(object, groupings, fun = c("sum",
  "mean"))

# S4 method for SummarizedExperiment
aggregateRows(object, col = "aggregate",
  fun = c("sum", "mean"))

# S4 method for matrix
aggregateCols(object, groupings, fun = c("sum",
  "mean"))

# S4 method for sparseMatrix
aggregateCols(object, groupings, fun = c("sum",
  "mean"))

# S4 method for SummarizedExperiment
aggregateCols(object, col = "aggregate",
  fun = c("sum", "mean"))

# S4 method for SingleCellExperiment
aggregateCols(object, fun = c("sum",
  "mean"))

Arguments

object

Object.

groupings

factor. Defines the 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. 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, with aggregated rows (features) or columns (samples).

Details

aggregateRows() works down the rows, and is designed to aggregate features (e.g. genes or transcripts). Most commonly, the aggregateRows() function can be used to aggregate counts from transcript-level to gene-level.

aggregateCols() works across the columns, and is designed to aggregate sample replicates.

Methods (by class)

  • matrix, sparseMatrix: Aggregate rows or columns using a grouping factor.

  • SummarizedExperiment: Aggregate rows or columns of 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.

See also

Examples

## Example data ==== assay <- matrix( data = c( 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 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 = "_" ) ) ) class(assay)
#> [1] "matrix"
print(assay)
#> sample1_replicate1 sample1_replicate2 sample2_replicate1 #> transcript1 0 1 1 #> transcript2 1 0 1 #> transcript3 1 1 0 #> transcript4 1 1 1 #> sample2_replicate2 #> transcript1 1 #> transcript2 1 #> transcript3 1 #> transcript4 0
genes <- factor(paste0("gene", rep(seq_len(2L), each = 2L))) names(genes) <- rownames(assay) print(genes)
#> transcript1 transcript2 transcript3 transcript4 #> gene1 gene1 gene2 gene2 #> Levels: gene1 gene2
samples <- factor(paste0("sample", rep(seq_len(2L), each = 2L))) names(samples) <- colnames(assay) print(samples)
#> sample1_replicate1 sample1_replicate2 sample2_replicate1 sample2_replicate2 #> sample1 sample1 sample2 sample2 #> Levels: sample1 sample2
## sparseMatrix sparse <- as(assay, "sparseMatrix") class(sparse)
#> [1] "dgCMatrix" #> attr(,"package") #> [1] "Matrix"
print(sparse)
#> 4 x 4 sparse Matrix of class "dgCMatrix" #> sample1_replicate1 sample1_replicate2 sample2_replicate1 #> transcript1 . 1 1 #> transcript2 1 . 1 #> transcript3 1 1 . #> transcript4 1 1 1 #> sample2_replicate2 #> transcript1 1 #> transcript2 1 #> transcript3 1 #> transcript4 .
## SummarizedExperiment se <- SummarizedExperiment::SummarizedExperiment( assay = list(assay = assay), colData = S4Vectors::DataFrame( sampleName = as.factor(names(samples)), aggregate = samples ), rowData = S4Vectors::DataFrame(aggregate = genes) ) print(se)
#> class: SummarizedExperiment #> dim: 4 4 #> metadata(0): #> assays(1): assay #> rownames(4): transcript1 transcript2 transcript3 transcript4 #> rowData names(1): aggregate #> colnames(4): sample1_replicate1 sample1_replicate2 sample2_replicate1 #> sample2_replicate2 #> colData names(2): sampleName aggregate
## aggregateRows ==== aggregateRows(assay, groupings = genes)
#> Aggregating counts using sum(). #> Groupings: #> transcript1 transcript2 transcript3 transcript4 #> gene1 gene1 gene2 gene2 #> Levels: gene1 gene2
#> sample1_replicate1 sample1_replicate2 sample2_replicate1 #> gene1 1 1 2 #> gene2 2 2 1 #> sample2_replicate2 #> gene1 2 #> gene2 1
aggregateRows(sparse, groupings = genes)
#> Aggregating counts using sum(). #> Groupings: #> transcript1 transcript2 transcript3 transcript4 #> gene1 gene1 gene2 gene2 #> Levels: gene1 gene2
#> 2 x 4 sparse Matrix of class "dgCMatrix" #> sample1_replicate1 sample1_replicate2 sample2_replicate1 #> gene1 1 1 2 #> gene2 2 2 1 #> sample2_replicate2 #> gene1 2 #> gene2 1
aggregateRows(se)
#> Aggregating counts using sum(). #> Groupings: #> transcript1 transcript2 transcript3 transcript4 #> gene1 gene1 gene2 gene2 #> Levels: gene1 gene2
#> class: SummarizedExperiment #> dim: 2 4 #> metadata(0): #> assays(1): '' #> rownames(2): gene1 gene2 #> rowData names(0): #> colnames(4): sample1_replicate1 sample1_replicate2 sample2_replicate1 #> sample2_replicate2 #> colData names(2): sampleName aggregate
## aggregateCols ==== aggregateCols(assay, groupings = samples)
#> Aggregating counts using sum(). #> Groupings: #> sample1_replicate1 sample1_replicate2 sample2_replicate1 sample2_replicate2 #> sample1 sample1 sample2 sample2 #> Levels: sample1 sample2
#> sample1 sample2 #> transcript1 1 2 #> transcript2 1 2 #> transcript3 2 1 #> transcript4 2 1
aggregateCols(sparse, groupings = samples)
#> Aggregating counts using sum(). #> Groupings: #> sample1_replicate1 sample1_replicate2 sample2_replicate1 sample2_replicate2 #> sample1 sample1 sample2 sample2 #> Levels: sample1 sample2
#> 4 x 2 sparse Matrix of class "dgCMatrix" #> sample1 sample2 #> transcript1 1 2 #> transcript2 1 2 #> transcript3 2 1 #> transcript4 2 1
aggregateCols(se)
#> Aggregating counts using sum(). #> Groupings: #> sample1_replicate1 sample1_replicate2 sample2_replicate1 sample2_replicate2 #> sample1 sample1 sample2 sample2 #> Levels: sample1 sample2
#> class: SummarizedExperiment #> dim: 4 2 #> metadata(1): aggregate #> assays(1): '' #> rownames(4): transcript1 transcript2 transcript3 transcript4 #> rowData names(1): aggregate #> colnames(2): sample1 sample2 #> colData names(0):