maldini425
maldini425

Reputation: 317

Thoughts on Generating an Age Variable Based on Years

I am trying to create a dummy variable for years. Currently, my data has a birth_date and a program start_date for each observation. I have been able to create a variable measuring an individual's age in days, but what I am actually looking for is a variable: age_join_date that tells me the following:

Individual birth_date    start_date  age_at_join_date
A          1990-12-31    2010-12-31  20 yrs old

B          1990-12-31    2011-12-31  21 yrs old

Essentially what I care about is one's age at the time they joined the program, rather than their actual age.

Upvotes: 0

Views: 126

Answers (1)

rdornas
rdornas

Reputation: 652

Your question was not really clear to me, but I think you can achieve the expected result using some lubridate functions, such as the interval operator %--% and years of the respective interval.

library(lubridate)
library(dplyr)

tibble::tribble(
  ~Individual,  ~birth_date,  ~start_date,
  "A", "31/12/1990", "31/12/2010",
  "B", "31/12/1990", "31/12/2011"
) %>% 
  mutate_at(vars(ends_with("date")), dmy) %>%  #just making date columns as date
  mutate(age_at_join_date = birth_date %--% start_date/years(1))

#> # A tibble: 2 x 4
#>   Individual birth_date start_date age_at_join_date
#>   <chr>      <date>     <date>                <dbl>
#> 1 A          1990-12-31 2010-12-31               20
#> 2 B          1990-12-31 2011-12-31               21

Created on 2020-02-12 by the reprex package (v0.3.0)

---UPDATING THE ANSWER TO REFLECT MORE RECENT TIDYVERSE SYNTAX---

library(lubridate)
library(dplyr)

tibble::tribble(
  ~Individual,  ~birth_date,  ~start_date,
  "A", "31/12/1990", "31/12/2010",
  "B", "31/12/1990", "31/12/2011"
) %>% 
  mutate(across(ends_with("date"), dmy),  #just making date columns as date
         age_at_join_date = birth_date %--% start_date/years(1))
#> # A tibble: 2 × 4
#>   Individual birth_date start_date age_at_join_date
#>   <chr>      <date>     <date>                <dbl>
#> 1 A          1990-12-31 2010-12-31               20
#> 2 B          1990-12-31 2011-12-31               21

Created on 2021-09-16 by the reprex package (v2.0.1)

Upvotes: 3

Related Questions