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.

 

 

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.

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


14 Comments. Leave new

  • Susan George
    March 4, 2021 1:59 pm

    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

    Reply
    • 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

      Reply
    • 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.

      Reply
  • thank you for this code! can you provide code for it to include calculation to 2 or 3 decimal places?

    Reply
    • 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.

      interval(date_1, date_2) %/% days(1)

      I hope that helps!

      Joachim

      Reply
  • > date1 date2 interval(start = date1, end = date2 ) %% months(1)
    [1] 2022-06-21 UTC–2022-06-21 UTC Not working here for me

    Reply
    • Hey Jawad,

      Are your dates properly formatted to the Date class?

      Regards,
      Joachim

      Reply
    • 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)

      Reply
      • 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

        Reply
  • 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

    Reply
    • 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:

      library(dplyr)
      library(lubridate)
       
      d <- data.frame(date_col = as.Date(c('2022-01-01', '2022-03-01', '2022-08-26')))
       
      d %>% mutate(datediff = interval(date_col, lead(date_col)) %>% months(1))
       
      #     date_col              datediff
      # 1 2022-01-01  5097600m 0d 0H 0M 0S
      # 2 2022-03-01 15379200m 0d 0H 0M 0S
      # 3 2022-08-26                  <NA>

      Regards,
      Cansu

      Reply
  • How would you calculate the reporting period age on a quarter basis between two dates for example, when calculating the age in quarters between a loss date and a book date, so let’s say the loss is 1/1/2023 and the reported date is 2/1/2023, for quarterly loss development triangles, usually if the quarter of the book date is the same as the loss date, the age would be 3 months. However, if the reported date was 4/1/2023 instead the age would be 6 months instead. Let me know if my question is not clear, I can show you the formula used in Excel to calculate the ages.

    Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top