Thoughts on Generating an Age Variable Based on Years

不想你离开。 提交于 2021-02-05 11:30:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!