Get Difference Between Dates in Years in R (2 Examples)
In this R programming tutorial you’ll learn how to calculate the difference between two dates in years.
Table of contents:
Let’s get started:
Creation of Example Data
The first step is to create some example data:
date1 <- as.Date("2027-05-17") # Create first example date date1 # Print first example date # [1] "2027-05-17"
date2 <- as.Date("2022-10-01") # Create second example date date2 # Print second example date # [1] "2022-10-01"
As you can see based on the previous outputs of the RStudio console, we have created two data objects with the Date class.
Note that we must have created our dates using the as.Date function. If your data is not formatted as a date properly, please have a look here.
Anyway, let’s move on to the examples!
Example 1: Calculate Date Difference in Years Using difftime() & time_length() Functions
The following code explains how to return the time difference between two dates in years.
For this, we first have to install and load the lubridate package, in order to use the corresponding functions.
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate package
In the next step, we can use the difftime function in combination with the time_length function of the lubridate package to get the difference between our two example dates in years:
years_diff <- time_length(difftime(date1, date2), "years") # Calculate difference in years years_diff # Print difference in years # [1] 4.62423
The previous output of the RStudio console shows our result: The time difference between our two dates is 4.62423 years.
As you can see, our result is not an integer, but a numerical value with many digits. Next, I’ll show how to change that!
Example 2: Rounding Date Difference in Years Using round() Function
In this section, I’ll show how to round our time difference in years to an integer value.
For this task, we can apply the round function as shown below:
years_diff_round <- round(years_diff) # Round difference in years years_diff_round # Print rounded difference # [1] 5
The rounded difference in years is five.
Video, Further Resources & Summary
Do you need more explanations on the content of this tutorial? Then I recommend having a look at the following video instruction on my YouTube channel. I illustrate the R code of this tutorial in the video tutorial.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might read some of the other articles on this homepage.
- Number of Months Between Two Dates
- Time Difference Between Dates in Weeks, Days, Hours, Minutes & Seconds
- R Programming Examples
To summarize: This tutorial has demonstrated how to compute the date difference in years in the R programming language. In case you have any additional questions, please tell me about it in the comments section.
Statistics Globe Newsletter