-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed lubridate dependency entirely by creating the is_date functio…
…n and adjusting calc_age. Fixed an error in calc_age due to the approximation approach it took. Would lead to odd results over time due to leap years and dividing by 365.25. Now looks directly at years, months, days and compared. Specifically uses importFrom for dependencies now across other functions for clarity.
- Loading branch information
Neil Currie
authored and
Neil Currie
committed
Nov 14, 2024
1 parent
8bc3b6e
commit 5883471
Showing
7 changed files
with
99 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
#' Is Date | ||
#' | ||
#' Determine if an object is a date | ||
#' | ||
#' @param x object to test, probably a vector. | ||
#' | ||
#' @return TRUE or FALSE | ||
#' @export | ||
|
||
is_date <- function (x) { | ||
|
||
class(x) == "Date" | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
test_that("calc_age works", { | ||
|
||
# Ages work | ||
|
||
expect_equal(calc_age(birth_date = as.Date("2022-07-01"), | ||
to_date = as.Date("2023-03-01")), | ||
0) | ||
|
||
expect_equal(calc_age(birth_date = as.Date("2013-03-01"), | ||
to_date = as.Date("2023-03-01")), | ||
10) | ||
|
||
expect_equal(calc_age(birth_date = as.Date("2013-03-02"), | ||
to_date = as.Date("2023-03-01")), | ||
9) | ||
|
||
# An unusual case that would be wrong if we took an approximation approach | ||
# and was wrong in an old version of the function | ||
|
||
expect_equal(calc_age(as.Date("2000-03-01"), as.Date("2021-02-28")), 20) | ||
|
||
# Multiple ages work | ||
|
||
birth_dates <- as.Date(c("2022-07-01", "2013-03-01", "2013-03-02")) | ||
to_dates <- as.Date(c("2023-03-01", "2022-03-01", "2021-01-01")) | ||
|
||
expect_equal(calc_age(birth_dates, to_dates), c(0, 9, 7)) | ||
expect_equal(calc_age(birth_dates, as.Date("2023-03-01")), c(0, 10, 9)) | ||
|
||
# Bad input types fail | ||
|
||
expect_error(calc_age(birth_date = data.frame(as.Date("2013-03-02")), | ||
to_date = as.Date("2023-03-01"))) | ||
|
||
expect_error(calc_age(birth_date = as.Date("2013-03-02"), | ||
to_date = data.frame(as.Date("2023-03-01")))) | ||
|
||
# if to_date not length 1 or length = length birth date then gives errors | ||
|
||
expect_error(calc_age(birth_dates, to_dates[-3])) | ||
|
||
# to_date < birth_date gives warnings and returns NA | ||
|
||
birth_dates[1] <- as.Date("2024-01-01") | ||
expect_warning(calc_age(birth_dates, to_dates, warn = TRUE)) | ||
|
||
expect_equal( | ||
calc_age(birth_dates, to_dates, set_negative_na = TRUE), c(NA, 9, 7) | ||
) | ||
|
||
|
||
|
||
}) |