Time Flies? Time Travels! Adding Days to Dates in R (Like a Pro)

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


Ever wished you could skip ahead a few days for that weekend getaway, or rewind to relive a magical moment? While real-life time travel remains a sci-fi dream, in R, adding days to dates is a breeze! Today, we’ll explore both base R and the powerful lubridate and timetk packages to master this handy skill.


Example 1: Base R Basics

Let’s start with the classic. Imagine you have a date stored as my_date <- "2024-01-31" (yes, today!). To add, say, 5 days, you can simply use my_date + 5. Voila! You’ve time-jumped to February 5th, 2024. But wait, this doesn’t handle months or leap years like a pro.

# Create a date object
date <- as.Date("2024-01-31")

# Add 5 days to the date
new_date <- date + 5

[1] "2024-01-31"
[1] "2024-02-05"
[1] "Date"
[1] "Date"

Example 2: Enter lubridate

This superhero package offers functions like as.Date() and days() that understand the nuances of dates. Let’s revisit our example:


my_date <- as.Date("2024-01-31") # Convert string to Date object
future_date <- my_date + days(5) # Add 5 days using days()

future_date # "2024-02-05"
[1] "2024-02-05"

See the magic? days(5) tells R to add 5 days specifically. You can even subtract days (imagine reliving that delicious pizza!):

pizza_day <- as.Date("2024-01-27") # Date of pizza bliss
relive_pizza <- pizza_day - days(2) # Travel back 2 days

relive_pizza # "2024-01-25"
[1] "2024-01-25"

Example 3: Beyond Days: timetk Takes the Wheel

Want to add weeks, months, or even years? timetk takes things to the next level with functions like years(), wednesdays(), and more. Check this out:


graduation <- as.Date("2025-06-15") # Your graduation date (hopefully!)

graduation %+time% "1 hour 34 seconds"
[1] "2025-06-15 01:00:34 UTC"
graduation %+time% "3 months"
[1] "2025-09-15"
graduation %+time% "1 year 3 months 6 days"
[1] "2026-09-21"
# Backward (Minus Time)
graduation %-time% "1 hour 34 seconds"
[1] "2025-06-14 22:59:26 UTC"
graduation %-time% "3 months"
[1] "2025-03-15"
graduation %-time% "1 year 3 months 6 days"
[1] "2024-03-09"

Bonus Tip: Don’t forget about formatting! Use format() with options like "%Y-%m-%d" to display your dates in your preferred format.

Now it’s your turn!

  • Try adding different units (weeks, months) to existing dates.
  • Use these techniques in your R scripts to analyze time-series data.
  • Challenge yourself: Can you calculate the difference between two dates in days or weeks?

Remember, practice makes perfect. The more you play with dates in R, the more comfortable you’ll become with this essential skill. So go forth, explore, and conquer the realm of time in R!

P.S. Share your coolest date-manipulation tricks in the comments below. Let’s learn from each other and keep the R community thriving!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)