Calculate child's age in months based on the date of birth and the date of data collection.
Arguments
- dos
A vector of class
Date
for the date of data collection. If the class is different than expected, the function will stop execution and return an error message indicating the type of mismatch.- dob
A vector of class
Date
for the child's date of birth. If the class is different than expected, the function will stop execution and return an error message indicating the type of mismatch.
Value
A vector of class numeric
for child's age in months. Any value less
than 6.0 and greater than or equal to 60.0 months will be set to NA
.
Examples
## Take two vectors of class "Date" ----
surv_date <- as.Date(
c(
"2024-01-05", "2024-01-05", "2024-01-05", "2024-01-08", "2024-01-08",
"2024-01-08", "2024-01-10", "2024-01-10", "2024-01-10", "2024-01-11"
)
)
bir_date <- as.Date(
c(
"2022-04-04", "2021-05-01", "2023-05-24", "2017-12-12", NA,
"2020-12-12", "2022-04-04", "2021-05-01", "2023-05-24", "2020-12-12"
)
)
## Apply the function ----
get_age_months(
dos = surv_date,
dob = bir_date
)
#> [1] 21.059548 32.164271 7.425051 NA NA 36.862423 21.223819
#> [8] 32.328542 7.589322 36.960986