Aggregate columns

aggregateCols(x, ...)

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

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

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

# S4 method for SingleCellExperiment
aggregateCols(x, fun)

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 columns 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 = "_" ) ) ) samples <- factor(paste0("sample", rep(seq_len(2L), each = 2L))) names(samples) <- colnames(counts) print(samples)
#> sample1_replicate1 sample1_replicate2 sample2_replicate1 sample2_replicate2 #> sample1 sample1 sample2 sample2 #> Levels: sample1 sample2
## 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
aggregateCols(counts, by = samples)
#> sample1 sample2 #> transcript1 2 4 #> transcript2 2 4 #> transcript3 4 2 #> transcript4 4 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 .
aggregateCols(sparse, by = samples)
#> 4 x 2 sparse Matrix of class "dgCMatrix" #> sample1 sample2 #> transcript1 2 4 #> transcript2 2 4 #> transcript3 4 2 #> transcript4 4 2
## SummarizedExperiment ==== se <- SummarizedExperiment::SummarizedExperiment( assays = SimpleList(counts = counts), colData = DataFrame( sampleName = as.factor(names(samples)), aggregate = samples ) ) print(se)
#> class: SummarizedExperiment #> dim: 4 4 #> metadata(0): #> assays(1): counts #> rownames(4): transcript1 transcript2 transcript3 transcript4 #> rowData names(0): #> colnames(4): sample1_replicate1 sample1_replicate2 sample2_replicate1 #> sample2_replicate2 #> colData names(2): sampleName aggregate
aggregateCols(se)
#> class: SummarizedExperiment #> dim: 4 2 #> metadata(1): aggregate #> assays(1): counts #> rownames(4): transcript1 transcript2 transcript3 transcript4 #> rowData names(0): #> colnames(2): sample1 sample2 #> colData names(0):