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

Closes #2001 Added processing for missing age_units in compute_age_years #2009

Merged
merged 14 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## Updates of Existing Functions
- The functions `derive_param_bmi()` and `derive_param_bsa()` are updated to have the option of producing more values at visits when only weight is collected (#1228).
- The functions `derive_var_age_years` and `compute_age_years` are updated to return an `NA` age in the case that the age unit is missing. The argument `unit` for `derive_vars_aage` is also changed to `age_unit` for consistency between these age-related functions (#2001).

## Breaking Changes
- The following functions, which were deprecated in previous `{admiral}` versions, have been removed: (#1950)
Expand Down
23 changes: 16 additions & 7 deletions R/compute_age_years.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
#' as `"years"` and `"Years"`).
#'
#' Permitted Values: `"years"`, `"months"`, `"weeks"`, `"days"`, `"hours"`, `"minutes"`,
#' `"seconds"`.
#' `"seconds", NA_character_`.
#'
#' @details Returns a numeric vector of ages in years as doubles. Note, underlying
#' computations assume an equal number of days in each year (365.25).
#' @details Returns a numeric vector of ages in years as doubles. Note
#' that passing `NA_character_` as a unit will result in an `NA` value for the outputted
#' age. Also note, underlying computations assume an equal number of days in each
#' year (365.25).
#'
#' @return The ages contained in `age` converted to years.
#'
Expand All @@ -34,8 +36,8 @@
#' )
#'
#' compute_age_years(
#' age = c(10, 520, 3650),
#' age_unit = c("YEARS", "WEEKS", "DAYS")
#' age = c(10, 520, 3650, 1000),
#' age_unit = c("YEARS", "WEEKS", "DAYS", NA_character_)
#' )
#'
compute_age_years <- function(age,
Expand All @@ -57,9 +59,16 @@ compute_age_years <- function(age,
))
}

if (length(age_unit) == 1) {
age_unit_rep <- rep(age_unit, length(age))
} else {
age_unit_rep <- age_unit
}

age_years <- time_length(
duration(age,
units = tolower(age_unit)
duration(
num = if_else(is.na(age_unit_rep), NA_real_, age),
units = if_else(is.na(age_unit_rep), "years", tolower(age_unit_rep))
),
unit = "years"
)
Expand Down
8 changes: 4 additions & 4 deletions R/derive_vars_aage.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#'
#' Default: `RANDDT`
#'
#' @param unit Unit
#' @param age_unit Age unit
#'
#' The age is derived in the specified unit
#'
Expand Down Expand Up @@ -64,12 +64,12 @@
derive_vars_aage <- function(dataset,
start_date = BRTHDT,
end_date = RANDDT,
unit = "years") {
age_unit = "years") {
start_date <- assert_symbol(enexpr(start_date))
end_date <- assert_symbol(enexpr(end_date))
assert_data_frame(dataset, required_vars = expr_c(start_date, end_date))
assert_character_scalar(
unit,
age_unit,
values = c("years", "months", "weeks", "days", "hours", "minutes", "seconds")
)

Expand All @@ -79,7 +79,7 @@ derive_vars_aage <- function(dataset,
new_var_unit = AAGEU,
start_date = !!start_date,
end_date = !!end_date,
out_unit = unit,
out_unit = age_unit,
add_one = FALSE,
trunc_out = TRUE
)
Expand Down
6 changes: 3 additions & 3 deletions docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ articles:
queries_dataset: queries_dataset.html
questionnaires: questionnaires.html
visits_periods: visits_periods.html
last_built: 2023-05-31T14:16Z
last_built: 2023-07-19T10:42Z
urls:
reference: https://pharmaverse.github.io/admiral/cran-release/reference
article: https://pharmaverse.github.io/admiral/cran-release/articles
reference: https://pharmaverse.github.io/admiral/cran-release/reference/
article: https://pharmaverse.github.io/admiral/cran-release/articles/

12 changes: 7 additions & 5 deletions man/compute_age_years.Rd

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

4 changes: 2 additions & 2 deletions man/derive_vars_aage.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/test-compute_age_years.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ test_that("compute_age_years Test 1: compute_age_years() works when `age_unit` i

## Test 2: compute_age_years() works when `age_unit` is a vector ----
test_that("compute_age_years Test 2: compute_age_years() works when `age_unit` is a vector", {
age_input <- c(28, 1461, 10227)
age_unit_input <- c("YEARS", "WEEKS", "DAYS")
age_input <- c(28, 1461, 10227, 32)
age_unit_input <- c("YEARS", "WEEKS", "DAYS", NA_character_)

expected_output <- rep(28, 3)
expected_output <- c(28, 28, 28, NA)

expect_equal(
compute_age_years(
Expand Down