Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edits from Test df to array #282

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
})
Loading