Wrangle child's age for downstream analysis. This includes calculating age
in months based on the date of data collection and the child's date of birth, and
setting to NA
the age values that are less than 6.0 and greater than or equal
to 60.0 months old.
Arguments
- df
A dataset of class
data.frame
to wrangle age from.- dos
A vector of class
Date
for date of data collection from thedf
. Default isNULL
.- dob
A vector of class
Date
for child's date of birth from thedf
. Default isNULL
.- age
A vector of class
numeric
of child's age in months. In most cases this will be estimated using local event calendars; in some other cases it can be a mix of the former and the one based on the child's date of birth and the date of data collection.- .decimals
The number of decimals places to which the age should be rounded. Default is 2.
Value
A data.frame
based on df
. The variable age
will be automatically
filled in each row where age value was missing and both the child's
date of birth and the date of data collection are available. Rows where age
is less than 6.0 and greater than or equal to 60.0 months old will be set to NA
.
Additionally, a new variable for df
named age_days
, of class double
, will
be created.
Examples
## A sample data ----
df <- data.frame(
surv_date = as.Date(c(
"2023-01-01", "2023-01-01", "2023-01-01", "2023-01-01", "2023-01-01"
)),
birth_date = as.Date(c(
"2019-01-01", NA, "2018-03-20", "2019-11-05", "2021-04-25"
)),
age = c(NA, 36, NA, NA, NA)
)
## Apply the function ----
mw_wrangle_age(
df = df,
dos = surv_date,
dob = birth_date,
age = age,
.decimals = 3
)
#> # A tibble: 5 × 4
#> surv_date birth_date age age_days
#> <date> <date> <dbl> <dbl>
#> 1 2023-01-01 2019-01-01 48 1461
#> 2 2023-01-01 NA 36 1096.
#> 3 2023-01-01 2018-03-20 57.4 1748
#> 4 2023-01-01 2019-11-05 37.9 1153
#> 5 2023-01-01 2021-04-25 20.2 616