使用具有疏水功能的突变体计算年龄 [英] Calculating age using mutate with lubridate functions
问题描述
我想根据出生日期计算年龄。
I would like to calculate age based on birth date.
如果我使用lubridate,我只需运行以下内容,如高效准确的年龄计算(以年,月,或周)在R给出生日期和任意日期
If I use lubridate, I would just run the following as in Efficient and accurate age calculation (in years, months, or weeks) in R given birth date and an arbitrary date
as.period(new_interval(start = birthdate,end = givendate) )$ year
但是,当我尝试在<$ c中使用 mutate
$ c> dplyr 创建新变量,我遇到了一个错误。
However, when I tried to use mutate
in dplyr
to create the new variable, I ran into an error.
library(dplyr); library(lubridate)
birthdate <- ymd(c(NA, "1978-12-31", "1979-01-01", "1962-12-30"))
givendate <- ymd(c(NA, "2015-12-31", "2015-12-31", NA))
df <- data.frame(
birthdate = birthdate,
givendate = givendate)
以下工作虽然它提供了所有的日期和时间值。即年,月,日,小时,分和秒。
The following works though it gives all the date and time values. i.e. year, month, day, hour, minute and second.
df<-df %>% mutate(age=as.period(interval(start = birthdate, end = givendate)))
# df
# birthdate givendate age
# 1 <NA> <NA> <NA>
# 2 1978-12-31 2015-12-31 37y 0m 0d 0H 0M 0S
# 3 1979-01-01 2015-12-31 36y 11m 30d 0H 0M 0S
# 4 1962-12-30 <NA> <NA>
以下内容不起作用:
df<-df %>%
mutate(age=as.period(interval(start = birthdate, end = givendate))$year)
它给出错误:
错误在mutate_impl(.data,dots)中:无效的下标类型'closure'
Error in mutate_impl(.data, dots) : invalid subscript type 'closure'
我以为可能是因为缺少的值。所以,我试过:
I thought it might be because of the missing values. So, I tried:
df<-df %>%
mutate(age=as.period(interval(start = birthdate, end = givendate))) %>%
mutate(age=if_else(!is.na(age),age$year,age))
它也会出现错误:
mutate_impl中的错误(.data,dots):没有找到对象'age'
Error in mutate_impl(.data, dots) : object 'age' not found
推荐答案
code> do
We can use do
df %>%
mutate(age=as.period(interval(start = birthdate, end = givendate))) %>%
do(data.frame(.[setdiff(names(.), "age")],
age = ifelse(!is.na(.$age), .$age$year, .$age)))
# birthdate givendate age
#1 <NA> <NA> NA
#2 1978-12-31 2015-12-31 37
#3 1979-01-01 2015-12-31 36
#4 1962-12-30 <NA> NA
由于 as .period
带有期间
类,我们可能需要S4方法来提取它
As the as.period
comes with period
class, we may need S4 methods to extract it
df %>%
mutate(age=as.period(interval(start = birthdate, end = givendate))) %>%
.$age %>%
.@year %>%
mutate(df, age = .)
# birthdate givendate age
#1 <NA> <NA> NA
#2 1978-12-31 2015-12-31 37
#3 1979-01-01 2015-12-31 36
#4 1962-12-30 <NA> NA
这篇关于使用具有疏水功能的突变体计算年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!