diff --git a/NAMESPACE b/NAMESPACE index 4b4e63b..a18c887 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -47,6 +47,7 @@ exportMethods("totals<-") exportMethods(aggregate) exportMethods(all_assigned) exportMethods(any_assigned) +exportMethods(as.data.frame) exportMethods(as_amounts) exportMethods(as_composition) exportMethods(as_graph) diff --git a/R/AllGenerics.R b/R/AllGenerics.R index 3551377..3dbf389 100644 --- a/R/AllGenerics.R +++ b/R/AllGenerics.R @@ -183,8 +183,6 @@ setGeneric( ) # Extract ====================================================================== - - ## Subset ---------------------------------------------------------------------- #' Extract or Replace Parts of an Object #' @@ -272,6 +270,26 @@ setGeneric( def = function(object, value) standardGeneric("totals<-") ) +#' Coerce to a Data Frame +#' +#' @param x An \R object (typically, a [`CompositionMatrix-class`] object). +#' @param row.names A [`character`] vector giving the row names for the data +#' frame, or `NULL`. +#' @param optional A [`logical`] scalar: should the names of the variables in +#' the data frame be checked? +#' @param group_var A [`character`] string specifying the name of the column to +#' create for group attribution. +#' @param ... Currently not used. +#' @return +#' A [`data.frame`]. +#' @example inst/examples/ex-coerce.R +#' @author N. Frerebeau +#' @docType methods +#' @family mutators +#' @name as.data.frame +#' @rdname as.data.frame +NULL + #' Matrix Transpose #' #' @param x A [`CompositionMatrix-class`] object. diff --git a/R/coerce.R b/R/coerce.R index 24683d7..2c7d1c3 100644 --- a/R/coerce.R +++ b/R/coerce.R @@ -93,40 +93,74 @@ setMethod( # To data.frame ================================================================ #' @method as.data.frame CompositionMatrix #' @export -as.data.frame.CompositionMatrix <- function(x, ...) { - as.data.frame(methods::as(x, "matrix"), row.names = rownames(x)) +as.data.frame.CompositionMatrix <- function(x, row.names = rownames(x), + optional = FALSE, ...) { + as.data.frame(methods::as(x, "matrix"), row.names = row.names, optional = optional) } +#' @export +#' @rdname as.data.frame +#' @aliases as.data.frame,CompositionMatrix-method +setMethod("as.data.frame", "CompositionMatrix", as.data.frame.CompositionMatrix) + #' @method as.data.frame GroupedComposition #' @export -as.data.frame.GroupedComposition <- function(x, ..., group_var = ".group") { +as.data.frame.GroupedComposition <- function(x, row.names = rownames(x), + optional = FALSE, ..., + group_var = ".group") { z <- data.frame( methods::as(x, "matrix"), - row.names = rownames(x) + row.names = row.names, + check.names = !optional ) z[[group_var]] <- group_names(x) z } +#' @export +#' @rdname as.data.frame +#' @aliases as.data.frame,GroupedComposition-method +setMethod("as.data.frame", "GroupedComposition", as.data.frame.GroupedComposition) + #' @method as.data.frame LogRatio #' @export -as.data.frame.LogRatio <- function(x, ...) { - as.data.frame(methods::as(x, "matrix"), row.names = rownames(x)) +as.data.frame.LogRatio <- function(x, row.names = rownames(x), + optional = FALSE, ...) { + as.data.frame(methods::as(x, "matrix"), row.names = row.names, optional = optional) } +#' @export +#' @rdname as.data.frame +#' @aliases as.data.frame,LogRatio-method +setMethod("as.data.frame", "LogRatio", as.data.frame.LogRatio) + #' @method as.data.frame GroupedLogRatio #' @export -as.data.frame.GroupedLogRatio <- function(x, ..., group_var = ".group") { +as.data.frame.GroupedLogRatio <- function(x, row.names = rownames(x), + optional = FALSE, ..., + group_var = ".group") { z <- data.frame( methods::as(x, "matrix"), - row.names = rownames(x) + row.names = row.names, + check.names = !optional ) z[[group_var]] <- group_names(x) z } +#' @export +#' @rdname as.data.frame +#' @aliases as.data.frame,GroupedLogRatio-method +setMethod("as.data.frame", "GroupedLogRatio", as.data.frame.GroupedLogRatio) + #' @method as.data.frame OutlierIndex #' @export -as.data.frame.OutlierIndex <- function(x, ...) { - as.data.frame(x@standard, row.names = rownames(x)) +as.data.frame.OutlierIndex <- function(x, row.names = rownames(x), + optional = FALSE, ...) { + as.data.frame(x@standard, row.names = row.names, optional = optional) } + +#' @export +#' @rdname as.data.frame +#' @aliases as.data.frame,OutlierIndex-method +setMethod("as.data.frame", "OutlierIndex", as.data.frame.OutlierIndex) diff --git a/inst/examples/ex-coerce.R b/inst/examples/ex-coerce.R index c5c4f45..5a8a64d 100644 --- a/inst/examples/ex-coerce.R +++ b/inst/examples/ex-coerce.R @@ -11,5 +11,5 @@ totals(B) A2 <- as_amounts(B) ## Coerce to a data.frame -X <- data.frame(B) +X <- as.data.frame(B) head(X) diff --git a/man/as.data.frame.Rd b/man/as.data.frame.Rd new file mode 100644 index 0000000..08cabaa --- /dev/null +++ b/man/as.data.frame.Rd @@ -0,0 +1,80 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/AllGenerics.R, R/coerce.R +\docType{methods} +\name{as.data.frame} +\alias{as.data.frame} +\alias{as.data.frame,CompositionMatrix-method} +\alias{as.data.frame,GroupedComposition-method} +\alias{as.data.frame,LogRatio-method} +\alias{as.data.frame,GroupedLogRatio-method} +\alias{as.data.frame,OutlierIndex-method} +\title{Coerce to a Data Frame} +\usage{ +\S4method{as.data.frame}{CompositionMatrix}(x, row.names = rownames(x), optional = FALSE, ...) + +\S4method{as.data.frame}{GroupedComposition}( + x, + row.names = rownames(x), + optional = FALSE, + ..., + group_var = ".group" +) + +\S4method{as.data.frame}{LogRatio}(x, row.names = rownames(x), optional = FALSE, ...) + +\S4method{as.data.frame}{GroupedLogRatio}( + x, + row.names = rownames(x), + optional = FALSE, + ..., + group_var = ".group" +) + +\S4method{as.data.frame}{OutlierIndex}(x, row.names = rownames(x), optional = FALSE, ...) +} +\arguments{ +\item{x}{An \R object (typically, a \code{\linkS4class{CompositionMatrix}} object).} + +\item{row.names}{A \code{\link{character}} vector giving the row names for the data +frame, or \code{NULL}.} + +\item{optional}{A \code{\link{logical}} scalar: should the names of the variables in +the data frame be checked?} + +\item{...}{Currently not used.} + +\item{group_var}{A \code{\link{character}} string specifying the name of the column to +create for group attribution.} +} +\value{ +A \code{\link{data.frame}}. +} +\description{ +Coerce to a Data Frame +} +\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) + +## This allows to restore the source data +A2 <- as_amounts(B) + +## Coerce to a data.frame +X <- as.data.frame(B) +head(X) +} +\seealso{ +Other mutators: +\code{\link{mutators}}, +\code{\link{totals}()} +} +\author{ +N. Frerebeau +} +\concept{mutators} diff --git a/man/as_amounts.Rd b/man/as_amounts.Rd index 41a5243..4886bed 100644 --- a/man/as_amounts.Rd +++ b/man/as_amounts.Rd @@ -36,7 +36,7 @@ totals(B) A2 <- as_amounts(B) ## Coerce to a data.frame -X <- data.frame(B) +X <- as.data.frame(B) head(X) } \seealso{ diff --git a/man/as_composition.Rd b/man/as_composition.Rd index b08d315..870ff28 100644 --- a/man/as_composition.Rd +++ b/man/as_composition.Rd @@ -60,7 +60,7 @@ totals(B) A2 <- as_amounts(B) ## Coerce to a data.frame -X <- data.frame(B) +X <- as.data.frame(B) head(X) } \seealso{ diff --git a/man/mutators.Rd b/man/mutators.Rd index 035a055..4ed4c82 100644 --- a/man/mutators.Rd +++ b/man/mutators.Rd @@ -32,6 +32,7 @@ Getters and setters to retrieve or set parts of an object. } \seealso{ Other mutators: +\code{\link{as.data.frame}()}, \code{\link{totals}()} } \author{ diff --git a/man/totals.Rd b/man/totals.Rd index c3be161..f157860 100644 --- a/man/totals.Rd +++ b/man/totals.Rd @@ -49,11 +49,12 @@ totals(B) A2 <- as_amounts(B) ## Coerce to a data.frame -X <- data.frame(B) +X <- as.data.frame(B) head(X) } \seealso{ Other mutators: +\code{\link{as.data.frame}()}, \code{\link{mutators}} } \author{