aggregate: Aggregate

aggregateR Documentation

Aggregate

Description

Aggregate

Usage

aggregate(x, ...)

aggregateCols(x, ...)

aggregateRows(x, ...)

## S4 method for signature 'Matrix'
aggregate(x, by, fun = c("sum", "mean", "n"), MARGIN = 1L)

## S4 method for signature 'SummarizedExperiment'
aggregate(x, col = "aggregate", fun = "sum", MARGIN = 1L)

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

## S4 method for signature 'Matrix'
aggregateCols(x, ...)

## S4 method for signature 'SummarizedExperiment'
aggregateCols(x, ...)

## S4 method for signature 'matrix'
aggregateCols(x, ...)

## S4 method for signature 'Matrix'
aggregateRows(x, ...)

## S4 method for signature 'SummarizedExperiment'
aggregateRows(x, ...)

## S4 method for signature 'matrix'
aggregateRows(x, ...)

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.

MARGIN

integer(1-2). Dimension where the function will be applied. For a two-dimensional matrix: 1 indicates rows; 2 indicates columns; c(1, 2) indicates rows and columns.

col

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

...

Additional arguments.

Value

Modified object.

Methods (by class)

  • aggregate(SummarizedExperiment): Arguments pass through to matrix or Matrix method, depending on the class of matrix defined in requested assay.

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.

Note

Updated 2021-09-13.

Author(s)

Michael Steinbaugh, Rory Kirchner

See Also

  • stats::aggregate().

  • S4Vectors::aggregate().

  • Matrix.utils::aggregate.Matrix().

  • muscat::aggregateData().

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)

samples <- factor(paste0("sample", rep(seq_len(2L), each = 2L)))
names(samples) <- colnames(counts)
print(samples)

## matrix ====
object <- counts
print(object)
aggregate(object, by = genes, MARGIN = 1L)
aggregateRows(object, by = genes)
aggregate(object, by = samples, MARGIN = 2L)
aggregateCols(object, by = samples)

## Matrix ====
object <- as(counts, "sparseMatrix")
print(object)
aggregate(object, by = genes, MARGIN = 1L)
aggregateRows(object, by = genes)
aggregate(object, by = samples, MARGIN = 2L)
aggregateCols(object, by = samples)

## SummarizedExperiment ====
object <- SummarizedExperiment::SummarizedExperiment(
    assays = S4Vectors::SimpleList(
        "counts" = counts
    ),
    rowData = S4Vectors::DataFrame(
        "aggregate" = genes
    ),
    colData = S4Vectors::DataFrame(
        "sampleName" = as.factor(names(samples)),
        "aggregate" = samples
    )
)
print(object)
aggregate(object, MARGIN = 1L)
aggregateRows(object)
aggregate(object, MARGIN = 2L)
aggregateCols(object)

acidgenomics/r-acidexperiment documentation built on Jan. 17, 2024, 7:56 p.m.