From bcd1d06444fb059aea149820634447d36f69c681 Mon Sep 17 00:00:00 2001 From: Chen-Yang Su Date: Sat, 28 Nov 2026 00:48:35 -0800 Subject: [PATCH] Fix age(exact = TRUE) for dates of birth on 29 February (#346) --- DESCRIPTION | 4 ++-- NEWS.md | 4 +++- R/age.R | 10 ++++++---- tests/testthat/test-age.R | 14 ++++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c0179fc5a..69cbd7853 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 3.1.0.9031 -Date: 2026-11-22 +Version: 3.1.0.9032 +Date: 2026-11-27 Title: Antimicrobial Resistance Data Analysis Description: Functions to simplify and standardise antimicrobial resistance (AMR) data analysis and to work with microbial and antimicrobial properties by diff --git a/NEWS.md b/NEWS.md index 00ee08202..9015f3037 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 3.1.0.9031 +# AMR 3.1.0.9032 This version fixes a critical bug in WISCA that biased all coverage estimates of `wisca()` and `antibiogram(..., wisca = TRUE)` downwards since AMR 3.0.0. Users are strongly advised to re-run their WISCA analyses. @@ -33,6 +33,8 @@ This version fixes a critical bug in WISCA that biased all coverage estimates of * Fixed the misaligned arrows in the message on taxonomically renamed microorganisms in colour-supporting consoles, for old names whose author reference lacks "*et al.*" (#336) * `intrinsic_resistant` data set * Species groups and complexes are now listed for every antimicrobial to which all their species are intrinsically resistant. This adds 62 bug-drug combinations for the *Burkholderia cepacia* complex, coryneform bacteria, the *Enterobacter cloacae* complex (e.g., amoxicillin and amoxicillin/clavulanic acid), the *Klebsiella pneumoniae* complex, and coagulase-negative staphylococci, which affects `mo_is_intrinsic_resistant()`, `not_intrinsic_resistant()`, and the priors of `wisca()` (#338) +* `age()` + * Fixed `exact = TRUE` returning `NA` for dates of birth on 29 February when the reference date is in a non-leap year. The birthday now falls on 1 March in those years, as it already did for ages in whole years. ### Other * The documentation of `first_isolate()` now explains how to determine first isolates per any additional variable using `dplyr::group_by()`, and the bias this introduces when that variable is related to resistance (#345) diff --git a/R/age.R b/R/age.R index 624fc7f5d..66dcc5f1e 100755 --- a/R/age.R +++ b/R/age.R @@ -83,11 +83,13 @@ age <- function(x, reference = Sys.Date(), exact = FALSE, na.rm = FALSE, ...) { # add decimals if (exact == TRUE) { # get dates of `x` when `x` would have the year of `reference` + reference_year <- format(as.Date(reference), "%Y") + x_month_day <- format(as.Date(x), "-%m-%d") + # 29 February does not exist in non-leap years, so use 1 March like the integer ages above + reference_is_leap <- !is.na(as.Date(paste0(reference_year, "-02-29"), format = "%Y-%m-%d")) + x_month_day[x_month_day %in% "-02-29" & !reference_is_leap] <- "-03-01" x_in_reference_year <- as.POSIXlt( - paste0( - format(as.Date(reference), "%Y"), - format(as.Date(x), "-%m-%d") - ), + paste0(reference_year, x_month_day), format = "%Y-%m-%d" ) # get differences in days diff --git a/tests/testthat/test-age.R b/tests/testthat/test-age.R index 4c2e32911..87b30228b 100644 --- a/tests/testthat/test-age.R +++ b/tests/testthat/test-age.R @@ -61,6 +61,20 @@ test_that("test-age.R", { 1 ) + # born on 29 February: the birthday is on 1 March in non-leap years + expect_equal( + age( + x = "2000-02-29", + reference = c("2023-02-28", "2023-03-01", "2023-06-01", "2024-02-29"), + exact = TRUE + ), + c(22 + 364 / 365, 23, 23 + 92 / 365, 24) + ) + expect_identical( + age(x = "2000-02-29", reference = c("2023-02-28", "2023-03-01")), + c(22L, 23L) + ) + ages <- c(3, 8, 16, 54, 31, 76, 101, 43, 21)