Skip to content

Commit

Permalink
Allow to coerce a CompositionMatrix to a data.frame
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Oct 25, 2023
1 parent 2984d4b commit 6bb9f9b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand Down
20 changes: 20 additions & 0 deletions R/coerce.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ setMethod(
)

make_codes <- function(x) {
if (!any(duplicated(x))) return(x)
x <- tapply(
X = x,
INDEX = x,
Expand All @@ -141,6 +142,7 @@ make_dimnames <- function(x) {
)
}

# To Amounts ===================================================================
#' @export
#' @rdname as_amounts
#' @aliases as_amounts,CompositionMatrix-method
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion man/as_amounts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/as_composition.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions man/as_features.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6bb9f9b

Please sign in to comment.