Yes, you can add a year to a date in R using the lubridate package’s %m+% operator with years(1), or with base R’s seq.Date() function. The simplest method is date %m+% years(1), which correctly handles leap days (e.g., February 29 rolls to February 28).
Why Adding a Year Is Trickier Than It Sounds
Adding 365 days to a date is not the same as adding one calendar year. A calendar year has 365 or 366 days depending on leap years, and adding 365 days to March 1, 2024 (a leap year) gives February 28, 2025, not March 1, 2025. Most people expect “add one year” to mean “the same month and day next year,” which is why R’s base + operator does not directly support year addition—it only adds seconds, minutes, hours, days, and weeks. To add years in base R, you must use seq.Date() or the lubridate package.
Using lubridate (Recommended)
The lubridate package is part of the tidyverse and provides the %m+% operator for month and year arithmetic that handles edge cases like leap days and month-end rollovers. Here’s how to add one year:
library(lubridate)
date <- as.Date("2024-02-29")
date_plus_one_year <- date %m+% years(1)
print(date_plus_one_year) # [1] "2025-02-28"
The years(1) function creates a period object representing one year. The %m+% operator adds it to the date, rolling February 29 to February 28 in non-leap years. For March 31, adding one month would give April 30, not April 31—lubridate handles that too.
To add multiple years, change the argument: years(5). You can also subtract with %m-%.
Using Base R: seq.Date()
If you prefer not to load a package, base R’s seq.Date() can add years by specifying by = "year" and taking the second element:
date <- as.Date("2023-06-15")
date_plus_one_year <- seq(date, by = "year", length.out = 2)[2]
print(date_plus_one_year) # [1] "2024-06-15"
This works for most dates, but be aware that seq.Date() with by = "year" may not handle leap days exactly as expected. For example, starting from 2024-02-29, it returns 2025-03-01 (the next actual day after Feb 28), which might not be what you want. For precise calendar-year addition, lubridate is safer.
Handling Vectors of Dates
Both methods work with vectors of dates. For lubridate, you can add a vector of years or a single year to a vector:
dates <- as.Date(c("2020-01-31", "2021-06-30", "2022-12-31"))
dates_plus_2 <- dates %m+% years(2)
print(dates_plus_2) # [1] "2022-01-31" "2023-06-30" "2024-12-31"
With base R, you can use sapply() or vapply() to apply the seq method to each element, but it’s less elegant.
Common Pitfalls and Misconceptions
A frequent mistake is using date + years(1) without the %m+% operator. In lubridate, date + years(1) actually works for adding years because lubridate overloads the + operator, but it does not handle month-end rollovers the same way as %m+%—it may add the exact number of seconds/days. For example, as.Date("2024-02-29") + years(1) returns NA in some versions because February 29 doesn’t exist in 2025. Always use %m+% for calendar-aware addition.
Another misconception is that adding 365 days is equivalent to adding one year. It is not, as explained earlier. If you need to add exactly 365 days (e.g., for a warranty period), use date + 365 (which adds days) or date + days(365).
Also note that years() creates a period, not a duration. Periods are calendar-based, while durations are exact seconds. For adding years, periods are usually what you want.
Alternatives: Other Packages and Functions
Besides lubridate and base R, the clock package (by the same author as lubridate) offers add_years() which is even more precise. Example:
library(clock)
date <- as.Date("2024-02-29")
date_plus_one <- add_years(date, 1)
print(date_plus_one) # [1] "2025-02-28"
The data.table package also has %m+% for IDate objects, but it’s essentially the same as lubridate.
Frequently Asked Questions
How do I add one year to a date in R without installing a package?
Use seq.Date() as shown above. It’s a base R function, so no installation needed. However, for leap-day handling, you might need to write a custom function that checks if the resulting date is valid and adjusts it.
What is the difference between %m+% and + in lubridate?
The %m+% operator adds months or years in a calendar-aware way, rolling back to the last day of the month if the target day doesn’t exist (e.g., Feb 30 becomes Feb 28). The + operator with a period object also works but may not perform this rollover—it can produce NA or an unexpected date. Always use %m+% for month/year arithmetic.
How do I add years to a date stored as a character string?
First convert to a Date or POSIXct object using as.Date() or as.POSIXct(), then use lubridate or seq. For example: as.Date("2023-01-15") %m+% years(1).
Does adding a year to February 29 ever return February 29?
No, because February 29 only exists in leap years. In a non-leap year, lubridate returns February 28, and base R’s seq returns March 1. If you need to preserve the day as much as possible, use lubridate.
Can I add a fractional year, like 0.5 years?
Yes, but it’s ambiguous. years(0.5) is not allowed because years() expects an integer. Instead, add 6 months with months(6) or 182.5 days with days(182.5). For precise fractional years, consider using durations: date + dyears(0.5) which adds 182.62125 days (365.2425/2).
Why does my date become NA when I add a year?
This often happens when using + with years() on a leap day. The %m+% operator avoids this. Also check that your date is actually a Date object, not a character or factor.
Related
- SAP Logon 800 Data Entry: What It Means
- Free SMART Goal Excel Template: How to Build One
- Python Random Numbers: How to Generate Them
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Hide Column in Tableau: Steps and TipsTo hide a column in Tableau, right-click the column header in the view and select Hide. This removes…
- Select a Cell Shortcut: Top Excel Keyboard ShortcutsThe quickest way to select a cell in Excel is to click on it, but for keyboard-only users,…
- Minimize Tab Shortcut: Windows & Mac KeysThe fastest way to minimize a window (not just a tab) in Windows is Windows logo key +…
- Does Excel Online Have Goal Seek? What to Use InsteadNo. Excel for the web does not currently include the Goal Seek command, so you cannot run a…
- Shared Mailbox in Outlook: How to Add It on Windows and MacTo add a shared mailbox in Outlook, first make sure your Microsoft 365 or Exchange administrator has granted…
- Excel Shortcut Between Sheets: Windows, Mac, and WebIn Excel for Microsoft 365 on Windows, press Ctrl+Page Up to move to the previous sheet tab, or…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.