Number of Months Between Two Dates in R (Example)
In this tutorial, I’ll show how to compute the number of months between two different date objects in the R programming language.
The article will consist of one example for the counting of months. To be more precise, the tutorial consists of this content:
You’re here for the answer, so let’s get straight to the example.
Example Data
We’ll use the following data as basement for this R programming tutorial.
date_1 <- as.Date("2020-08-10") # Create example dates date_2 <- as.Date("2025-01-01")
Have a look at the previous R code. It shows that our example data are two date objects.
Example: Computing Month Difference Using lubridate Package
The following code illustrates how to count the number of months between two dates based on the interval and months functions of the lubridate package.
First, we have to install and load the lubridate add-on package:
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate package
Now, we can apply the interval and months to our two date objects as shown below:
interval(date_1, date_2) %/% months(1) # Apply interval & months # 52
As you can see based on the previously shown output of the RStudio console, the time difference between our two dates is 52 months.
Video & Further Resources
Do you need further information on the R programming codes of this article? Then you could have a look at the following video of my YouTube channel. In the video, I show the R programming code of this 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.
In addition to the video, you might have a look at the related posts on my website. You can find a selection of tutorials about the handling of dates below.
- How to Create a Range of Dates in R
- Convert Date to Day of Week in R
- Convert Date to Numeric Time Object in R
- as.Date Function in R
- The R Programming Language
Summary: In this post, I explained how to get the number of months between multiple dates in the R programming language. Let me know in the comments section, if you have further questions.
Statistics Globe Newsletter
12 Comments. Leave new
hi thank you for the above,
I have a quick question, I want to calculate the difference in dates, but both these dates are in the same column.
So, I have an ID column, visit column- with visit 1 and visit 2 and a date column. So, I want to know the difference in months between visit 1 and visit 2, but not all ID’s have a visit 2.If you can help me figure this.
Thanks
Hi Susan,
Could you provide some example data or a subset of your real data? It’s difficult to calculate without seeing the exact structure of the data.
You may either post it here or via email to joachim@statisticsglobe.com
Regards,
Joachim
Cast the data to wide, with columns: ID, visit1, visit2. Then visit1 will contain the date of the first visit, and where available, visit2 the date of the second. If there is no second visit, visit2 will be NA. Do the calculation of difference in dates.
Hey SDC,
Thanks a lot for your help regarding this question!
Regards
Joachim
thank you for this code! can you provide code for it to include calculation to 2 or 3 decimal places?
Hey Liz,
Thanks for your comment! This is difficult though, because every month has a different number of days.
You may use the following code to get the number of days between your dates, and then use this difference according to what you want to do with your data.
I hope that helps!
Joachim
> date1 date2 interval(start = date1, end = date2 ) %% months(1)
[1] 2022-06-21 UTC–2022-06-21 UTC Not working here for me
Hey Jawad,
Are your dates properly formatted to the Date class?
Regards,
Joachim
I had this happen to me because I was writing:
df$months_difference <- interval(df$date1, df$date2) %%months(1)
using <- instead of =, this worked:
df$months_difference = interval(df$date1, df$date2) %%months(1)
Hello Polina,
It is a bit weird that = works instead of <-. Both <- and = can be used for assignment operations in R. Maybe it is due to some settings in your R studio. Thank you for sharing. Best, Cansu
You could use lead() from the dplyr package:
library(dplyr)
library(lubridate)
d <- data.frame(date_col = as.Date(c('2022-01-01', '2022-03-01', '2022-08-26')))
ddif % mutate(datediff = interval(date_col, lead(date_col)) %/% months(1))
# date_col datediff
# 1 2022-01-01 2
# 2 2022-03-01 5
# 3 2022-08-26 NA
Hello Jens,
I think you wanted to provide a solution for finding intervals of months for multiple dates. Is that right? When I applied the code you shared, I got this result instead of what you shared:
Regards,
Cansu