Create an age variable to account for missing data

library(tidyverse)
library(lubridate)
(moderna <- tibble::tribble(
  ~ID, ~dob, ~vax1, ~vax2,
  "1", "7-27-2001", "6-30-2021", "10-13-2021",
  "2", "10-5-1971", "6-18-2021", NA,
  "3", "4-29-1977", "2-24-2021", "6-15-2021",
  "4", "8-21-2001", "9-2-2021", NA
))


moderna %>%
  mutate(across(-ID, mdy)) %>% # everything not ID is a mdy format date
  mutate(across(starts_with("vax"), # process the vax dates
    ~ as.numeric(as.period(interval(dob, .x), # calculate their interval from dob and return the year part
      unit = "year"
    ), "years"),
    .names = "ageyrs_{col}"
  ))

# A tibble: 4 x 6
  ID    dob        vax1       vax2       ageyrs_vax1 ageyrs_vax2
  <chr> <date>     <date>     <date>           <dbl>       <dbl>
1 1     2001-07-27 2021-06-30 2021-10-13        19.9        20.2
2 2     1971-10-05 2021-06-18 NA                49.7        NA  
3 3     1977-04-29 2021-02-24 2021-06-15        43.8        44.1
4 4     2001-08-21 2021-09-02 NA                20.0        NA

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.