Learn R Programming

nexus (version 0.4.0)

group_split: Divide into Groups

Description

Divides a compositional matrix by groups.

Usage

group_split(object, ...)

# S4 method for CompositionMatrix group_split(object, by, ...)

# S4 method for GroupedComposition group_split(object, ...)

# S4 method for GroupedLogRatio group_split(object, ...)

# S4 method for CompositionMatrix split(x, f, drop = FALSE, ...)

# S4 method for LogRatio split(x, f, drop = FALSE, ...)

Value

A list of CompositionMatrix objects.

Arguments

object, x

A CompositionMatrix object.

...

Currently not used.

by

A vector or a list of grouping elements, each as long as the variables in object (see group()).

f

A 'factor' in the sense that as.factor(f) defines the grouping, or a list of such factors in which case their interaction is used for the grouping (see base::split()).

drop

A logical scalar: should levels that do not occur be dropped?

Author

N. Frerebeau

See Also

Other grouping methods: group(), group_extract(), group_metadata

Examples

Run this code
## Create a data.frame
X <- data.frame(
  samples = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
  groups = c("X", "X", "X", "X", 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)

## Split by group
## /!\ Unassigned samples (NA) are discarded ! /!\
(s1 <- split(Y, f = X$groups))

## Better to use grouped matrix
(s2 <- group_split(Y, by = X$groups))

Z <- as_composition(X, groups = 2)
(s3 <- group_split(Z))

## Bind by rows
do.call(rbind, s3)

Run the code above in your browser using DataLab