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
ordata.frame
to be coerced.- ...
Currently not used.
- parts
A
vector
giving the index of the column to be used a compositional parts. IfNULL
andautodetect
isTRUE
(the default), allnumeric
columns will be used.- groups
An
integer
giving the index of the column to be used to group the samples. IfNULL
(the default), no grouping is stored.- autodetect
A
logical
scalar: shouldnumeric
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()
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] 246 297 234 401 136 281 367 159 185 256 194 228 230 147 223 230 237 211 234
#> [20] 236
## 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.06097561 0.34146341 0.21544715 0.19512195 0.18699187
#> S2 0.10774411 0.31649832 0.24242424 0.28619529 0.04713805
#> S3 0.35897436 0.33333333 0.11111111 0.07692308 0.11965812
#> S4 0.22693267 0.24438903 0.09226933 0.22693267 0.20947631
#> S5 0.47794118 0.04411765 0.11764706 0.17647059 0.18382353
#> S6 0.21352313 0.18149466 0.18149466 0.34875445 0.07473310