diff --git a/NAMESPACE b/NAMESPACE index 4120be7..915a597 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,7 @@ exportMethods(any_assigned) exportMethods(any_replicated) exportMethods(as_amounts) exportMethods(as_composition) +exportMethods(as_features) exportMethods(as_graph) exportMethods(barplot) exportMethods(closure) diff --git a/R/AllGenerics.R b/R/AllGenerics.R index 74d8e46..c02f4d7 100644 --- a/R/AllGenerics.R +++ b/R/AllGenerics.R @@ -67,6 +67,23 @@ setGeneric( valueClass = "matrix" ) +#' Coerce to Features +#' +#' Converts an object to a collection of features. +#' @param from A [`CompositionMatrix-class`] object. +#' @param ... Currently not used. +#' @return +#' A [`data.frame`] with all informations as extra columns. +#' @example inst/examples/ex-coerce.R +#' @author N. Frerebeau +#' @docType methods +#' @family compositional data tools +#' @aliases as_features-method +setGeneric( + name = "as_features", + def = function(from, ...) standardGeneric("as_features") +) + # Simplex ====================================================================== #' Operations in the Simplex #' diff --git a/R/coerce.R b/R/coerce.R index 411fb53..e38ee84 100644 --- a/R/coerce.R +++ b/R/coerce.R @@ -117,6 +117,7 @@ setMethod( ) make_codes <- function(x) { + if (!any(duplicated(x))) return(x) x <- tapply( X = x, INDEX = x, @@ -141,6 +142,7 @@ make_dimnames <- function(x) { ) } +# To Amounts =================================================================== #' @export #' @rdname as_amounts #' @aliases as_amounts,CompositionMatrix-method @@ -152,6 +154,24 @@ setMethod( } ) +# To Features ================================================================== +#' @export +#' @rdname as_features +#' @aliases as_features,CompositionMatrix-method +setMethod( + f = "as_features", + signature = c(from = "CompositionMatrix"), + definition = function(from) { + data.frame( + codes = get_identifiers(from), + samples = get_samples(from), + groups = get_groups(from), + from, + row.names = NULL + ) + } +) + # To data.frame ================================================================ #' @method as.data.frame CompositionMatrix #' @export diff --git a/man/as_amounts.Rd b/man/as_amounts.Rd index 539f85d..a43fe7d 100644 --- a/man/as_amounts.Rd +++ b/man/as_amounts.Rd @@ -41,7 +41,8 @@ head(X) } \seealso{ Other compositional data tools: -\code{\link{as_composition}()} +\code{\link{as_composition}()}, +\code{\link{as_features}()} } \author{ N. Frerebeau diff --git a/man/as_composition.Rd b/man/as_composition.Rd index 75f99ea..29c89a4 100644 --- a/man/as_composition.Rd +++ b/man/as_composition.Rd @@ -89,7 +89,8 @@ head(X) } \seealso{ Other compositional data tools: -\code{\link{as_amounts}()} +\code{\link{as_amounts}()}, +\code{\link{as_features}()} } \author{ N. Frerebeau diff --git a/man/as_features.Rd b/man/as_features.Rd new file mode 100644 index 0000000..a3abc83 --- /dev/null +++ b/man/as_features.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/AllGenerics.R, R/coerce.R +\docType{methods} +\name{as_features} +\alias{as_features} +\alias{as_features-method} +\alias{as_features,CompositionMatrix-method} +\title{Coerce to Features} +\usage{ +as_features(from, ...) + +\S4method{as_features}{CompositionMatrix}(from) +} +\arguments{ +\item{from}{A \code{\linkS4class{CompositionMatrix}} object.} + +\item{...}{Currently not used.} +} +\value{ +A \code{\link{data.frame}} with all informations as extra columns. +} +\description{ +Converts an object to a collection of features. +} +\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) + +## This allows to restore the source data +A2 <- as_amounts(B) + +## Coerce to a data.frame +X <- data.frame(B) +head(X) +} +\seealso{ +Other compositional data tools: +\code{\link{as_amounts}()}, +\code{\link{as_composition}()} +} +\author{ +N. Frerebeau +} +\concept{compositional data tools}