Skip to content

Commit

Permalink
Allow to enable/disable automatic detection of numeric variables
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Jan 5, 2025
1 parent 69a66f4 commit 8ddd952
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* Add `group()`, `ungroup()`, `group_levels()`, `group_names()`, `group_indices()`, `group_rows()`, `group_length()`, `group_size()`, `group_extract()`, `group_split()` and `is_grouped()` to work with grouped data.
* Add `transform_lr()`, `transform_clr()`, `transform_alr()`, `transform_ilr()`, `transform_plr()` and `transform_inverse()` methods for `GroupedComposition` and `GroupedLogRatio` objects.

## Enhancements
* `as_composition()` gained a new `autodetect` argument to enable/disable automatic detection of numeric variables.

## Breaking changes
* Redesign the internal mechanism for grouping data.
* `hist()` now produces a single histogram.
Expand Down
6 changes: 4 additions & 2 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ NULL
#' Coerces an object to a `CompositionMatrix` object.
#' @param from A [`matrix`] or [`data.frame`] to be coerced.
#' @param parts A `vector` giving the index of the column to be used a
#' compositional parts. If `NULL` (the default), all [`double`] columns will be
#' used.
#' compositional parts. If `NULL` and `autodetect` is `TRUE` (the default),
#' all `numeric` columns will be used.
#' @param groups An [`integer`] giving the index of the column to be used to
#' group the samples. If `NULL` (the default), no grouping is stored.
#' @param autodetect A [`logical`] scalar: should `numeric` variables be
#' automatically used as compositional parts?
#' @param verbose A [`logical`] scalar: should \R report extra information
#' on progress?
#' @param ... Currently not used.
Expand Down
21 changes: 14 additions & 7 deletions R/coerce.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ setMethod(
f = "as_composition",
signature = c(from = "matrix"),
definition = function(from) {
## Validation
arkhe::assert_type(from, "numeric")

## Make row/column names
lab <- make_names(x = NULL, n = nrow(from), prefix = "S")
rownames(from) <- if (has_rownames(from)) rownames(from) else lab
Expand All @@ -41,7 +44,7 @@ setMethod(
setMethod(
f = "as_composition",
signature = c(from = "data.frame"),
definition = function(from, parts = NULL, groups = NULL,
definition = function(from, parts = NULL, groups = NULL, autodetect = TRUE,
verbose = getOption("nexus.verbose")) {
## Clean row/column names
lab <- make_names(x = NULL, n = nrow(from), prefix = "S")
Expand All @@ -50,12 +53,16 @@ setMethod(

## Remove non-numeric columns
if (is.null(parts)) {
parts <- arkhe::detect(from, f = is.double, margin = 2) # Logical
if (isTRUE(verbose)) {
n <- sum(parts)
what <- ngettext(n, "Found %g part (%s)", "Found %g parts (%s)")
cols <- paste0(colnames(from)[parts], collapse = ", ")
message(sprintf(what, n, cols))
if (isTRUE(autodetect)) {
parts <- arkhe::detect(from, f = is.numeric, margin = 2)
if (isTRUE(verbose)) {
n <- sum(parts)
what <- ngettext(n, "Found %g part (%s)", "Found %g parts (%s)")
cols <- paste0(colnames(from)[parts], collapse = ", ")
message(sprintf(what, n, cols))
}
} else {
arkhe::assert_filled(parts)
}
} else {
if (is.numeric(parts)) parts <- seq_len(ncol(from)) %in% parts
Expand Down
8 changes: 6 additions & 2 deletions man/as_composition.Rd

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

2 changes: 1 addition & 1 deletion vignettes/groups.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ When coercing a `data.frame` to a `CompositionMatrix` object, **nexus** allows t
data("bronze", package = "folio")
## Use the third column (dynasties) for grouping
coda <- as_composition(bronze, groups = 3)
coda <- as_composition(bronze, parts = 4:11, groups = 3)
```

`groups(x)` and `groups(x) <- value` allow to retrieve or set groups of an existing `CompositionMatrix`. Missing values (`NA`) or empty strings can be used to specify that a sample does not belong to any group.
Expand Down

0 comments on commit 8ddd952

Please sign in to comment.