R Adding & Subtracting Months & Years from Date (2 Examples)
In this R tutorial you’ll learn how to add and subtract months or years from a Date object.
Example Data
x <- as.Date("2025-01-01") # Create example date x # Print date object # [1] "2025-01-01" |
x <- as.Date("2025-01-01") # Create example date x # Print date object # [1] "2025-01-01"
Example 1: How to Add & Subtract Months from a Date in R
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate package |
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate package
x %m+% months(5) # Adding five months # [1] "2025-06-01" |
x %m+% months(5) # Adding five months # [1] "2025-06-01"
x %m-% months(5) # Subtracting five months # [1] "2024-08-01" |
x %m-% months(5) # Subtracting five months # [1] "2024-08-01"
Example 2: How to Add & Subtract Years from a Date in R
x %m+% years(1) # Adding one year # [1] "2026-01-01" |
x %m+% years(1) # Adding one year # [1] "2026-01-01"
x %m-% years(1) # Subtracting one years # [1] "2024-01-01" |
x %m-% years(1) # Subtracting one years # [1] "2024-01-01"