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,
  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 (the default), all double 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.

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] 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