R: How to Calculate Age Using lubridate


Often you may want to calculate the age of an individual in R.

Fortunately this is easy to do by using the interval() function from the lubridate package in R, which is designed to perform this exact task.

The interval() function uses the following basic syntax:

interval(start = NULL, end = NULL)

where:

  • start: The starting date
  • end: The ending date

The following example shows how to use the interval() function from the lubridate package to calculate ages in practice.

Note: Before using the interval() function, you may need to first install the lubridate package by using the following syntax:

install.packages('lubridate')

Once the lubridate package is installed, you can use the interval() function.

Example: How to Calculate Age Using lubridate in R

Suppose we create the following data frame named df that contains information about various employees at some company:

#create data frame
df <- data.frame(birth_date=c('2001-01-03', '1995-02-15', '1990-05-09',
                              '2002-08-10', '1980-10-14', '1967-12-30'),
                 sales=c(130, 98, 120, 88, 94, 100))

#view data frame
df

  birth_date sales
1 2022-01-03   130
2 2022-02-15    98
3 2023-05-09   120
4 2023-08-10    88
5 2024-10-14    94
6 2024-12-30   100

This data frame contains the following columns:

  • birth_date: The birth date of the employee
  • sales: The total sales made by the employee during their tenure

Suppose that we would like to calculate the age of each employee in the data frame.

To do so, we will need to calculate the difference between their birth date and the current date.

We can use the interval() function from the lubridate package to do so:

library(lubridate)

##add new column that calculates age of employee
df$age <- as.period(interval(start=df$birth_date, end=Sys.Date()))

#view updated data frame
df

  birth_date sales                 age
1 2001-01-03   130 23y 4m 25d 0H 0M 0S
2 1995-02-15    98 29y 3m 13d 0H 0M 0S
3 1990-05-09   120 34y 0m 19d 0H 0M 0S
4 2002-08-10    88 21y 9m 18d 0H 0M 0S
5 1980-10-14    94 43y 7m 14d 0H 0M 0S
6 1967-12-30   100 56y 4m 28d 0H 0M 0S

Notice that a new column has been added to the data frame named age that contains the age of each employee.

For example:

  • The first employee is 23 years, 4 months and 25 days old.
  • The second employee is 29 years, 3 months and 13days old.
  • The third employee is 34 years, 0 months and 19 days old.

And so on.

Note that the interval() function includes the hours, minutes and seconds for the age of the person as well.

Note that the Sys.Date() function returns the current date, which allows to use the current date as the “end date” in the interval() function to calculate the current age of each employee in the data frame.

Note that we could also use the year() function to only extract the year from each value in the resulting age() column if we would like:

library(lubridate)

##add new column that calculates age of employee in years
df$age <- year(as.period(interval(start=df$birth_date, end=Sys.Date())))

#view updated data frame
df

  birth_date sales age
1 2001-01-03   130  23
2 1995-02-15    98  29
3 1990-05-09   120  34
4 2002-08-10    88  21
5 1980-10-14    94  43
6 1967-12-30   100  56

Notice that the age column now only contains the years of age for each corresponding employee instead of including the months, days, hours, minutes and seconds.

Note: You can find the complete documentation for the interval() function from the lubridate package here.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Generate a Sequence of Dates with Lubridate in R
How to Convert Character to Date Using Lubridate in R
How to Get First or Last Day of Month Using Lubridate in R

Leave a Reply

Your email address will not be published. Required fields are marked *