Skip to contents

Coerces an object to a CompositionMatrix object.

Usage

as_composition(from, ...)

# S4 method for class 'numeric'
as_composition(from)

# S4 method for class 'matrix'
as_composition(from)

# S4 method for class 'data.frame'
as_composition(
  from,
  parts = NULL,
  groups = NULL,
  autodetect = TRUE,
  verbose = getOption("nexus.verbose")
)

Arguments

from

A matrix or data.frame to be coerced.

...

Currently not used.

parts

A vector giving the index of the column to be used a compositional parts. If NULL and autodetect is TRUE (the default), all numeric columns will be used.

groups

An integer giving the index of the column to be used to group the samples. If NULL (the default), no grouping is stored.

autodetect

A logical scalar: should numeric variables be automatically used as compositional parts?

verbose

A logical scalar: should R report extra information on progress?

Value

A CompositionMatrix object.

Details

See vignette("nexus").

See also

Other compositional data tools: as_amounts()

Author

N. Frerebeau

Examples

## Create a count matrix
A1 <- matrix(data = sample(1:100, 100, TRUE), nrow = 20)

## Coerce to compositions
B <- as_composition(A1)

## Row sums are internally stored before coercing to relative frequencies
totals(B)
#>  [1] 223 260 276 253 249 312 138 264 271 205 146 176 312 246 297 234 401 136 281
#> [20] 367

## This allows to restore the source data
A2 <- as_amounts(B)

## Coerce to a data.frame
X <- as.data.frame(B)
head(X)
#>           V1         V2        V3         V4         V5
#> S1 0.3183857 0.04484305 0.1793722 0.14798206 0.30941704
#> S2 0.3153846 0.16538462 0.3615385 0.11153846 0.04615385
#> S3 0.0942029 0.34420290 0.3115942 0.09057971 0.15942029
#> S4 0.2529644 0.23715415 0.2845850 0.13438735 0.09090909
#> S5 0.1325301 0.25301205 0.0562249 0.34939759 0.20883534
#> S6 0.2884615 0.18589744 0.2147436 0.24679487 0.06410256