user17582908
user17582908

Reputation:

Finding the number of years (age) between 2 date variables in R

I have two variables:

The first variable is BIRTH_DATE in Day, Month, Year format. Ex. 31-Oct-76

The second is EVENT_DATE in Day, Month, Year as well. Ex. 25-Aug-04

I want to create a new variable called "Age" that is calculated as the number of years between these two dates. How can I do this?

Thanks!

Upvotes: 0

Views: 3456

Answers (3)

TichPi
TichPi

Reputation: 146

Please see this example which may be short and elegant:

library(lubridate)

#generate example data
df=data.frame(BIRTH_DATE= rep(dmy("31-Oct-76"),5),
EVENT_DATE=rep(dmy("25-Aug-04"),5))

#number of year
df$year=interval(df$BIRTH_DATE, df$EVENT_DATE) / years(1)

df
#  BIRTH_DATE EVENT_DATE     year
#1 1976-10-31 2004-08-25 27.81694
#2 1976-10-31 2004-08-25 27.81694
#3 1976-10-31 2004-08-25 27.81694
#4 1976-10-31 2004-08-25 27.81694
#5 1976-10-31 2004-08-25 27.81694

Upvotes: 0

Andre Wildberg
Andre Wildberg

Reputation: 19088

This gives you the full year value

BIRTH_DATE <- as.Date("31-Oct-76", format="%d-%b-%y")
EVENT_DATE <- as.Date("25-Aug-04", format="%d-%b-%y")

Age <- length(seq.Date(from=BIRTH_DATE, to=EVENT_DATE, by="year"))-1
Age
[1] 27

Use apply to use it on a data frame

df$Age <- apply(df, 1, function(x) 
  length(seq.Date(from=as.Date(x["BIRTH_DATE"], format="%d-%b-%y"),
                    to=as.Date(x["EVENT_DATE"], format="%d-%b-%y"), by="year"))-1)
df
  BIRTH_DATE EVENT_DATE Age
1  31-Oct-76  25-Aug-04  27
2  31-Oct-76  25-Aug-04  27
3  31-Oct-76  25-Aug-04  27
4  31-Oct-76  25-Aug-04  27
5  31-Oct-76  25-Aug-04  27

Upvotes: 1

Sweepy Dodo
Sweepy Dodo

Reputation: 1873

df$diff_years <- 

as.numeric(difftime(as.Date(df$EVENT_DATE, format="%d-%b-%y")
                    , as.Date(df$BIRTH_DATE, format="%d-%b-%y")
                    ,unit="weeks"
                    )
           ) / 52.25

Upvotes: 1

Related Questions