Skip to contents

Splits the data into subsets, computes summary statistics for each, and returns the result.

Usage

# S4 method for CompositionMatrix
aggregate(x, by, FUN, ..., simplify = TRUE, drop = TRUE)

Arguments

x

A CompositionMatrix object.

by

A vector or a list of grouping elements, each as long as the variables in x. The elements are coerced to factors before use.

FUN

A function to compute the summary statistics.

...

Further arguments to be passed to FUN.

simplify

A logical scalar: should the results be simplified to a matrix if possible?

drop

A logical scalar indicating whether to drop unused combinations of grouping values.

Value

A matrix.

See also

Other statistics: covariance(), dist, mahalanobis(), margin(), mean(), metric_var(), quantile(), scale(), variation()

Author

N. Frerebeau

Examples

## Create a data.frame
X <- data.frame(
  samples = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
  groups = c("X", "X", "X", NA, NA, NA, "Y", "Y", "Y"),
  Ca = c(7.72, 7.32, 3.11, 7.19, 7.41, 5, 4.18, 1, 4.51),
  Fe = c(6.12, 5.88, 5.12, 6.18, 6.02, 7.14, 5.25, 5.28, 5.72),
  Na = c(0.97, 1.59, 1.25, 0.86, 0.76, 0.51, 0.75, 0.52, 0.56)
)

## Coerce to a compositional matrix
Y <- as_composition(X)

## Compositional mean by sample
aggregate(Y, by = get_samples(Y), FUN = mean)
#>          Ca        Fe         Na
#> A 0.4468157 0.4539051 0.09927924
#> B 0.4746506 0.4742013 0.05114805
#> C 0.3067545 0.6238351 0.06941036

## Metric variance by group
aggregate(Y, by = get_groups(Y), FUN = metric_var)
#>        [,1]
#> X 0.1978911
#> Y 0.4275836