问题
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.
回答1:
You question was not really clear to me, but I think you can achieve the expected result using some lubridate
functions, 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)
来源:https://stackoverflow.com/questions/60178598/thoughts-on-generating-an-age-variable-based-on-years