Add & Subtract Days to & from Date in R (2 Examples)
In this tutorial, I’ll explain how to add or subtract a certain number of days from a date object in the R programming language.
The article looks as follows:
You’re here for the answer, so let’s get straight to the examples:
Creation of Example Data
The following data will be used as basement for this R programming tutorial:
my_date <- "2021-10-05" # Create example date my_date # Print example date # [1] "2021-10-05"
As you can see based on the previous output of the RStudio console, our example data contains a single date formatted as a character string.
Let’s check the class of our date:
class(my_date) # Check class of date # [1] "character"
Our example date has the character class.
In order to add or subtract values from our date, we have to convert our date to the Date class first:
my_date_new <- as.Date(my_date) # Convert character to Date my_date_new # Print updated date # [1] "2021-10-05"
Let’s check the class of our new data object:
class(my_date_new) # Class of updated date # [1] "Date"
Our new data object has the class Date.
Example 1: Add Number to Date Object
This example explains how to add a certain number of days to our date object.
For this, we can simply use the + sign as shown below:
my_date_new + 100 # Add to date # [1] "2022-01-13"
As you can see, we have added 100 days to our date object.
Example 2: Subtract Number from Date Object
Similar to Example 1, we can subtract days from our date using the – sign:
my_date_new - 100 # Subtract from date # [1] "2021-06-27"
The previous R code has subtracted 100 days from our date.
Video & Further Resources
In case you need further explanations on the R programming codes of this article, you may want to watch the following video of my YouTube channel. I illustrate the contents of this tutorial in the video:
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, you may have a look at the other articles on this homepage. I have released several articles already.
- Time Difference Between Dates in Weeks, Days, Hours, Minutes & Seconds
- Change Format of Dates in R
- Order Data Frame by Date in R
- R Programming Overview
In this R tutorial you learned how to add or subtract days from dates. In case you have further questions, don’t hesitate to let me know in the comments. Besides that, please subscribe to my email newsletter in order to get updates on new tutorials.
Statistics Globe Newsletter
8 Comments. Leave new
Hi. Very good portal. Can you tell how to add/substract days from posixct objects?
Hey Anil,
Thank you for the kind words!
You may use the lubridate package to add/subtract days from a POSIXct object:
Regards,
Joachim
Your way of presentation is very simple yet powerful. Thank you Sir.
Thanks a lot Unnikrishnan, glad you like my way of explaining! 🙂
This is the best place for me to find R syntax . Very helpful 🙂
Hey Geeth,
This is really great to hear! 🙂 Thank you very much for the nice comment!
Regards
Joachim
Is there a command to edit the year specifically? Adding 365 using your method is one way but can I specifically target the Year section
Hey Nadya,
Please have a look at this tutorial: https://statisticsglobe.com/add-subtract-months-years-from-date-in-r
Regards,
Joachim