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
matrixordata.frameto be coerced.- ...
Currently not used.
- parts
A
vectorgiving the index of the column to be used a compositional parts. IfNULLandautodetectisTRUE(the default), allnumericcolumns will be used.- groups
An
integergiving the index of the column to be used to group the samples. IfNULL(the default), no grouping is stored.- autodetect
A
logicalscalar: shouldnumericvariables be automatically used as compositional parts?- verbose
A
logicalscalar: should R report extra information on progress?
Value
A CompositionMatrix object.
Details
See vignette("nexus").
See also
Other compositional data tools:
as_amounts()
Examples
## Create a count matrix
A1 <- matrix(data = sample(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] 203 159 270 193 188 117 246 279 273 298 268 283 146 319 239 220 154 289 230
#> [20] 284
## 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.03448276 0.1379310 0.2019704 0.32512315 0.300492611
#> S2 0.04402516 0.0754717 0.3396226 0.45911950 0.081761006
#> S3 0.02222222 0.3296296 0.2259259 0.15555556 0.266666667
#> S4 0.02590674 0.1502591 0.4559585 0.06217617 0.305699482
#> S5 0.06382979 0.2500000 0.3297872 0.03191489 0.324468085
#> S6 0.06837607 0.2393162 0.2991453 0.38461538 0.008547009
