基于年份生成年龄变量的思考 [英] Thoughts on Generating an Age Variable Based on Years
问题描述
我多年来一直在尝试创建一个虚拟变量.目前,我的数据对于每个观察都有一个birth_date 和一个program start_date.我已经能够创建一个以天为单位测量个人年龄的变量,但我实际寻找的是一个变量:age_join_date 告诉我以下内容:
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.
推荐答案
你的问题我不是很清楚,但我认为你可以使用一些 lubridate
函数来达到预期的结果,作为间隔运算符 %--%
和 years
各自的间隔.
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
由 reprex 包 (v0.3.0) 于 2020 年 2 月 12 日创建上>
Created on 2020-02-12 by the reprex package (v0.3.0)
这篇关于基于年份生成年龄变量的思考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!