Skip to content

Commit

Permalink
added logicals to custom.subsets
Browse files Browse the repository at this point in the history
  • Loading branch information
TGuillerme committed Jan 15, 2024
1 parent e59f566 commit 688d10b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Authors@R: c(person("Thomas", "Guillerme", role = c("aut", "cre", "cph"),
person("Jack", "Hatfield", role = c("aut", "cph"))
)
Maintainer: Thomas Guillerme <[email protected]>
Version: 1.8.1
Date: 2023-12-15
Version: 1.8.2
Date: 2024-01-15
Description: A modular package for measuring disparity (multidimensional space occupancy). Disparity can be calculated from any matrix defining a multidimensional space. The package provides a set of implemented metrics to measure properties of the space and allows users to provide and test their own metrics. The package also provides functions for looking at disparity in a serial way (e.g. disparity through time) or per groups as well as visualising the results. Finally, this package provides several statistical tests for disparity analysis.
Depends:
R (>= 3.6.0),
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
dispRity v1.8.1 (2023-12-15)
dispRity v1.8.2 (2024-01-15)
=========================

### MINOR IMPROVEMENTS

* `custom.subsets` can now take a logical vector for

### BUG FIXES

* `scale.dispRity` now correctly ignores `NA`s when scaling.
Expand Down
4 changes: 2 additions & 2 deletions R/custom.subsets.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @description Splits the data into a customized subsets list.
#'
#' @param data A \code{matrix} or a \code{list} of matrices.
#' @param group Either a \code{list} of row numbers or names to be used as different groups, a \code{data.frame} with the same \eqn{k} elements as in \code{data} as rownames or a \code{factor} vector. If \code{group} is a \code{phylo} object matching \code{data}, groups are automatically generated as clades (and the tree is attached to the resulting \code{dispRity} object).
#' @param group Either a \code{list} of row numbers or names to be used as different groups, a \code{data.frame} with the same \eqn{k} elements as in \code{data} as rownames, a \code{factor} or a \code{logical} vector. If \code{group} is a \code{phylo} object matching \code{data}, groups are automatically generated as clades (and the tree is attached to the resulting \code{dispRity} object).
#' @param tree \code{NULL} (default) or an optional \code{phylo} or \code{multiPhylo} object to be attached to the data.
#'
#' @details
Expand Down Expand Up @@ -93,7 +93,7 @@ custom.subsets <- function(data, group, tree = NULL) {
}

## Sanitize the group variable
group_class <- check.class(group, c("matrix", "data.frame", "list", "phylo", "factor"))
group_class <- check.class(group, c("matrix", "data.frame", "list", "phylo", "factor", "logical"))
if(group_class == "phylo") {
## Saving the tree for export
tree <- group
Expand Down
10 changes: 8 additions & 2 deletions R/custom.subsets_fun.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ set.group.list <- function(group, data, group_class) {
group <- as.data.frame(group)
}

## Logical is set to factor
if(group_class[1] == "logical") {
group <- as.factor(group)
group_class[1] <- "factor"
}

## Switch methods
return(switch(group_class,
## Group is already a list
Expand All @@ -56,9 +62,9 @@ set.group.list <- function(group, data, group_class) {
unlist(group_list, recursive = FALSE)},
## Group is a phylo
"phylo" = get.tree.clades(group, data),
## Group is factor
"factor" = {group_list <- lapply(as.list(levels(group)), function(lvl, group) which(group == lvl), group = group) ; names(group_list) <- levels(group) ; group_list}
)
)
))
}


Expand Down
2 changes: 1 addition & 1 deletion man/custom.subsets.Rd

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

1 change: 1 addition & 0 deletions tests/testthat/test-as.covar.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Test
nocov <- TRUE
#package_coverage(type = "tests", quiet = FALSE, clean = FALSE)
test_that("as.covar works in standalone", {

Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-custom.subsets.R
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,16 @@ test_that("custom.subsets works with a factor", {
expect_is(test, "dispRity")
expect_equal(n.subsets(test), 3)
expect_equal(size.subsets(test), c("gulls" = 159, "plovers" = 98, "sandpipers" = 102))
})

test_that("custom.subsets works with a logical", {
## Random 3D dataset with 200 taxa
data <- dispRity::space.maker(elements = 200, dimensions = 3, distribution = rnorm)
set.seed(1)
group <- sample(c(TRUE, FALSE), 200, replace = TRUE)

## Creating groups with a logical
expect_warning(test <- custom.subsets(data, group = group))
expect_equal(name.subsets(test), c("FALSE", "TRUE"))
expect_equal(size.subsets(test), c("FALSE" = 98, "TRUE" = 102))
})

0 comments on commit 688d10b

Please sign in to comment.