Aggregate

aggregate(x, ...)

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

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

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.

...

Additional arguments.

Value

Modified object.

Note

Updated 2020-05-22.

See also

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
aggregate(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 .
aggregate(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