Fix age(exact = TRUE) for dates of birth on 29 February (#346)

This commit is contained in:
Chen-Yang Su authored and GitHub committed 2026-11-28 09:48:35 +01:00
1 parent c7f885d5e4
commit bcd1d06444
4 files changed
+25 -7

No files matched your search

+2 -2
View File
@@ -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
+3 -1
View File
@@ -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)
+6 -4
View File
@@ -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
+14
View File
@@ -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)