Skip to content

Commit

Permalink
Merge pull request #282 from UCD-SERG/test-df_to_array
Browse files Browse the repository at this point in the history
edits from Test df to array
  • Loading branch information
d-morrison authored Sep 25, 2024
2 parents d82381a + 2211e0a commit 81d571b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# serocalculator (development version)

* created unit test for `df_to_array()`
* fixed `dplyr::select()` deprecation warning in `df_to_array()`
* Generalized `get_()` methods from `pop_data`-specific to `default`.

# serocalculator 1.2.0
Expand Down
20 changes: 13 additions & 7 deletions R/df_to_array.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#'
#' @keywords internal
#'
df.to.array <- function(
df.to.array <- function( # nolint: object_name_linter
df,
dim_var_names,
value_var_name = "value") {
Expand All @@ -18,10 +18,14 @@ df.to.array <- function(

#' Convert a data.frame (or tibble) into a multidimensional array
#'
#' @param df a [data.frame()] (or [tibble::tibble()]) in long format (each row contains one value for the intended array)
#' @param dim_var_names a [character()] vector of variable names in `df`. All of these variables should be factors, or a warning will be produced.
#' @param value_var_name a [character()] variable containing a variable name from `df` which contains the values for the intended array.
#' @return an [array()] with dimensions defined by the variables in `df` listed in `dim_var_names`
#' @param df a [data.frame()] (or [tibble::tibble()]) in long format
#' (each row contains one value for the intended array)
#' @param dim_var_names a [character()] vector of variable names in `df`.
#' All of these variables should be factors, or a warning will be produced.
#' @param value_var_name a [character()] variable containing a variable name
#' from `df` which contains the values for the intended array.
#' @return an [array()] with dimensions defined by the variables in `df`
#' listed in `dim_var_names`
#'
#' @examples
#' library(dplyr)
Expand All @@ -33,7 +37,9 @@ df.to.array <- function(
#' cols = c("Sepal.Length", "Sepal.Width", "Petal.Width", "Petal.Length")
#' ) %>%
#' mutate(parameter = factor(parameter, levels = unique(parameter)))
#' arr <- df %>% serocalculator:::df_to_array(dim_var_names = c("parameter", "Species"))
#' arr <- df %>%
#' serocalculator:::df_to_array(
#' dim_var_names = c("parameter", "Species"))
#' ftable(arr[,,1:5])
#' @noRd
df_to_array <- function(
Expand Down Expand Up @@ -77,6 +83,6 @@ df_to_array <- function(
)

df %>%
mutate(.by = all_of(dim_var_names), obs = 1:n()) %>%
mutate(.by = all_of(dim_var_names), obs = row_number()) %>%
xtabs(formula = formula(xtabs_formula))
}
10 changes: 8 additions & 2 deletions tests/testthat/test-df_to_array.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ test_that("df_to_array() produces consistent results", {
df <- iris %>%
tidyr::pivot_longer(
names_to = "parameter",
cols = c("Sepal.Length", "Sepal.Width", "Petal.Width", "Petal.Length")
cols = c(
"Sepal.Length",
"Sepal.Width",
"Petal.Width",
"Petal.Length"
)
) %>%
mutate(parameter = factor(parameter, levels = unique(parameter)))
arr <- df %>% serocalculator:::df_to_array(dim_var_names = c("parameter", "Species"))
arr <- df %>%
serocalculator:::df_to_array(dim_var_names = c("parameter", "Species"))
arr %>% expect_snapshot_value(style = "serialize")
})

0 comments on commit 81d571b

Please sign in to comment.