Add & Subtract Months & Years to/from Date Object in R (2 Examples)

 

In this tutorial you’ll learn how to add and subtract months or years from a Date object in the R programming language.

Table of contents:

With that, let’s get started:

 

Constructing Example Data

The first step is to create some data that we can use in the examples below:

my_date <- as.Date("2022-10-01")    # Create example date
my_date                             # Print date object
# [1] "2022-10-01"

The previous output of the RStudio console shows the structure of our example data: It’s a single Date object stored in the data object my_date.

Note that we have used the as.Date function to create this Date object. We can check whether this worked well using the class function:

class(my_date)                      # Check class of date
# [1] "Date"

The data class of our data object is the Date class. If your data is not having this class you should convert it to the Date class using the as.Date function first.

 

Example 1: Add or Subtract Months from a Date Object

Example 1 shows how to add or subtract months from our Date object.

For this, we first need to install and load the lubridate package:

install.packages("lubridate")       # Install & load lubridate package
library("lubridate")

Now, we can use the %m+% operator and the months function to add one month to our date as shown below:

my_date %m+% months(1)              # Add one month
# [1] "2022-11-01"

The number specified within the months function defines the number of months we want to add. For instance, we can add 30 months like this:

my_date %m+% months(30)             # Add 30 months
# [1] "2025-04-01"

If we want to subtract months from our date, we can use the %m-% operator. The following R code subtracts ten months from our date:

my_date %m-% months(10)             # Subtract 10 months
# [1] "2021-12-01"

 

Example 2: Add or Subtract Years from a Date Object

In this section, I’ll explain how to add and subtract years from our Date object. For this, we have to use the %m+% operator and the years function.

The following R code adds five years to our date…

my_date %m+% years(5)               # Add years
# [1] "2027-10-01"

…and the following R syntax subtracts five years from our date:

my_date %m-% years(5)               # Subtract years
# [1] "2017-10-01"

 

Video & Further Resources

I have recently published a video on my YouTube channel, which shows the R programming syntax of this article. You can find the video below:

 

 

Furthermore, you may want to have a look at the other RStudio posts of this website:

 

In this R programming tutorial you have learned how to add/subtract months/years to/from a date. If you have additional questions or comments, don’t hesitate to let me know in the comments.

 

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.


2 Comments. Leave new

  • Hallo,

    hier ist ein kleiner Fehlerteufel:

    The number specified within the months function defines the number of months we want to add. For instance, we can add 30 days like this:

    my_date %m+% months(30) # Add 30 months
    # [1] “2025-04-01”

    Du meintest sicher “We can add 30 months”, nicht “we can aqdd 30 days” …

    Reply
    • Hi Jens,

      vielen Dank für dein wachsames Auge! Gib gerne Bescheid, falls dir in Zukunft weitere Fehler auffallen sollten 🙂

      Ich habe den Fehler soeben behoben.

      Viele Grüße

      Joachim

      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