Skip to contents

Coerces an object to a CompositionMatrix object.

Usage

as_composition(from, ...)

# S4 method for numeric
as_composition(from)

# S4 method for matrix
as_composition(from)

# S4 method for data.frame
as_composition(
  from,
  codes = NULL,
  samples = NULL,
  groups = NULL,
  auto = getOption("nexus.autodetect"),
  verbose = getOption("nexus.verbose")
)

Arguments

from

A matrix or data.frame to be coerced.

...

Currently not used.

codes

An integer giving the index of the column to be used as laboratory codes (unique identifiers).

samples

An integer giving the index of the column to be used for sample identification: allows duplicates to identify replicated measurements. If NULL (the default), row names will be used as sample IDs.

groups

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

auto

A logical scalar: try to automatically detect codes, samples and groups columns?

verbose

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

Value

A CompositionMatrix object.

Details

The CompositionMatrix class has special slots:

When coercing a data.frame to a CompositionMatrix object, an attempt is made to automatically assign values to these slots by mapping column names (case insensitive, plural insensitive). This behavior can be disabled by setting options(nexus.autodetect = FALSE) or overridden by explicitly specifying the columns to be used.

See vignette("nexus").

Note

All non-numeric variable will be removed.

See also

Other compositional data tools: as_amounts(), as_features()

Author

N. Frerebeau

Examples

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

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

## Row sums are internally stored before coercing to relative frequencies
get_totals(B)
#>  [1] 245 264 258 162 253 285 223 369 255 279 190 246 344 381  84 231 240 356 195
#> [20] 145

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

## Coerce to a data.frame
X <- data.frame(B)
head(X)
#>            V1        V2         V3         V4        V5
#> S1 0.13469388 0.1673469 0.15918367 0.33877551 0.2000000
#> S2 0.13636364 0.3106061 0.04166667 0.21212121 0.2992424
#> S3 0.02713178 0.3643411 0.16279070 0.20155039 0.2441860
#> S4 0.14197531 0.1172840 0.24691358 0.35185185 0.1419753
#> S5 0.34782609 0.3003953 0.05928854 0.06719368 0.2252964
#> S6 0.11929825 0.1789474 0.28771930 0.30526316 0.1087719