Learn R Programming

mefa4 (version 0.3-2)

groupSums: Compute Summary Statistics of Data Subsets

Description

Compute summary statistics (sums, means) of data subsets.

Usage

groupSums(object, ...)
## S3 method for class 'matrix':
groupSums(object, MARGIN, by, na.rm = FALSE, ...)
## S3 method for class 'sparseMatrix':
groupSums(object, MARGIN, by, na.rm = FALSE, ...)
## S3 method for class 'Mefa':
groupSums(object, MARGIN, by, replace, na.rm = FALSE, ...)

groupMeans(object, ...) ## S3 method for class 'matrix': groupMeans(object, MARGIN, by, na.rm = FALSE, ...) ## S3 method for class 'sparseMatrix': groupMeans(object, MARGIN, by, na.rm = FALSE, ...) ## S3 method for class 'Mefa': groupMeans(object, MARGIN, by, replace, na.rm = FALSE, ...)

Arguments

object
an object.
MARGIN
numeric, 1 indicates rows are to be summed/averaged, 2 indicates columns are to be summed/averaged. c(1, 2) is not yet implemented, but can be calculated calling the function twice.
by
a vector of grouping elements corresponding to dimensions of object and MARGIN.
replace
a data frame to be used when applying the method on a "Mefa" object. The attribute table corresponding to MARGIN is dropped (NULL), replacement table can be specified via this argument.
na.rm
logical. Should missing values be removed? Sum is calculated by zeroing out NA values, mean is calculated as dividing the sum by the number of non-NA values when collapsing.
...
other argument, currently not implemented.

Value

  • An object similar to the input one.

Details

The method sums/averages cells in a matrix. The functions behind these methods use sparse matrices, so calculations can be more efficient compared to using aggregate.

See Also

rowSums, rowMeans, colSums, colMeans

Standard aggregate in package stats

aggregate.mefa in package mefa for S3 "mefa" objects.

Examples

Run this code
x <- data.frame(
    sample = paste("Sample", c(1,1,2,2,3,4), sep="."),
    species = c(paste("Species", c(1,1,1,2,3), sep="."), 
    "zero.pseudo"), count = c(1,2,10,3,4,0))
samp <- data.frame(samples=levels(x$sample), var1=1:2)
taxa <- data.frame(specnames=levels(x$species), var2=c("b","a"))
rownames(samp) <- samp$samples
rownames(taxa) <- taxa$specnames
x2 <- Xtab(count ~ sample + species, x, cdrop=FALSE,rdrop=TRUE)
x5 <- Mefa(x2, samp, taxa, join="inner")

groupSums(as.matrix(x2), 1, c(1,1,2))
groupSums(as.matrix(x2), 2, c(1,1,2,2))
groupSums(x2, 1, c(1,1,2))
groupSums(x2, 2, c(1,1,2,2))
groupSums(x5, 1, c(1,1,2))
groupSums(x5, 2, c(1,1,2,2))

groupMeans(as.matrix(x2), 1, c(1,1,2))
groupMeans(as.matrix(x2), 2, c(1,1,2,2))
groupMeans(x2, 1, c(1,1,2))
groupMeans(x2, 2, c(1,1,2,2))
groupMeans(x5, 1, c(1,1,2))
groupMeans(x5, 2, c(1,1,2,2))

Run the code above in your browser using DataLab