Skip to contents

Divides the compositional matrix x into the groups defined by f.

Usage

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

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

Arguments

x

A CompositionMatrix object.

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?

...

Currently not used.

Value

A list of CompositionMatrix objects.

See also

Other mutators: groups, identifiers, samples, subset(), totals

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", "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
split(Y, f = get_groups(Y))
#> $X
#> <CompositionMatrix: 4 x 3>
#>          Ca        Fe         Na
#> 1 0.5212694 0.4132343 0.06549629
#> 2 0.4949290 0.3975659 0.10750507
#> 3 0.3280591 0.5400844 0.13185654
#> 4 0.5052706 0.4342937 0.06043570
#> 
#> $Y
#> <CompositionMatrix: 3 x 3>
#>          Ca        Fe         Na
#> 7 0.4106090 0.5157171 0.07367387
#> 8 0.1470588 0.7764706 0.07647059
#> 9 0.4179796 0.5301205 0.05189991
#> 

## Split by sample
split(Y, f = get_samples(Y))
#> $A
#> <CompositionMatrix: 3 x 3>
#>          Ca        Fe         Na
#> 1 0.5212694 0.4132343 0.06549629
#> 2 0.4949290 0.3975659 0.10750507
#> 3 0.3280591 0.5400844 0.13185654
#> 
#> $B
#> <CompositionMatrix: 3 x 3>
#>          Ca        Fe         Na
#> 4 0.5052706 0.4342937 0.06043570
#> 5 0.5221987 0.4242424 0.05355884
#> 6 0.3952569 0.5644269 0.04031621
#> 
#> $C
#> <CompositionMatrix: 3 x 3>
#>          Ca        Fe         Na
#> 7 0.4106090 0.5157171 0.07367387
#> 8 0.1470588 0.7764706 0.07647059
#> 9 0.4179796 0.5301205 0.05189991
#>