Calculating age using mutate with lubridate functions

I would like to calculate age based on date of birth.

If I use lubridate, I would simply do the following, as in Effective and accurate calculation of age (in years, months or weeks) in R, date of birth date and arbitrary date

as.period(new_interval(start = birthdate, end = givendate))$year

However, when I tried to use mutatein dplyrto create a new variable, I encountered 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 steps, although giving 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>

The following does not work:

df<-df %>% 
       mutate(age=as.period(interval(start = birthdate, end = givendate))$year)

This gives an error:

Mutate_impl (.data, dots) file error: invalid closure index type

I thought this might be due to 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))

It also gives an error:

mutate_impl (.data, dots): ""

+4
3

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 period, S4

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
+1

lubridate,

  • Period - S4 ""
  • year - S3 .

. https://github.com/hadley/lubridate/blob/master/R/accessors-year.r) .

,

df %>% mutate(age = year(as.period(interval(start = birthdate, end = givendate))))
+5

We can use the function yearfrom lubridateto get the difference between two dates in years.

library(dplyr); library(lubridate)
df %>% mutate(age = year(givendate) - year(birthdate))

#   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
+2
source

Source: https://habr.com/ru/post/1667105/


All Articles