How to Add and Subtract Days to and from Date in R ? - GeeksforGeeks

21 Apr, 2021
R Programming Language provides a variety of ways for dealing with both date and date/time data. The built-in framework as.Date function is responsible for the handling of dates alone, the library chron in R handles both dates and times, without any support for time zones; whereas the POSIXct and POSIXlt classes provide the support for handling datetime objects as well as timezones. Easy conversion of date-time objects can be performed to other date-related objects.
Method 1: Using as.Date() method
The date objects are stored as the number of days calculated starting January 1, 1970, where negative numbers are used to refer to earlier dates. The Date objects support basic arithmetic directly, where in the integers are added or subtracted directly from the Dates. N number of days are added or subtracted directly and the standard date format is returned as an output. The Date object can also specify different formats to contain the dates. The as.Date() method takes as input a string date object and converts it to a Date object. 
as.Date(character date object)
The following R snippet illustrates the usage of Date objects : 
R
# declaring a date objectdate <- as.Date("2020/12/11")print ("Original Date")print (date)  # subtracting 3 days from date # objectn = 3  # subtracting days new_sub_date <- date - nprint ("Subtracted Date")print (new_sub_date)  # adding daysnew_add_date <- date + nprint ("Added Date")print (new_add_date)
Output:
[1] "Original Date"
[1] "2020-12-11"
[1] "Subtracted Date"
[1] "2020-12-08"
[1] "Added Date"
[1] "2020-12-14"
Method 2: lubridate package in R
Lubridate is an R package to simulate working easily with dates and time objects. It gives a wide range of functions to perform modifications on the day objects. ymd() method in R is used to extract the Date portion from the date-time object, which is converted into standard years, months, and days formats. days() method accepts an integer as an argument and performs arithmetic on the Date objects using mathematical operators, directly. 
Syntax: ymd(date)
Arguments : date – String date object
Returns : Date object in ymd format 
Code:
R
# using required librarieslibrary(lubridate)  # declaring a date objectdate <- "2009-10-01"print ("Original Date")print (date)  # subtracting 3 days from date# objectn = 3  # subtracting days new_sub_date <- date - nprint ("Subtracted Date")print (new_sub_date)  # adding daysnew_add_date <- date + nprint ("Added Date")print (new_add_date)
Output:
[1] "Original Date" 
[1] "2009-10-01" 
[1] "Subtracted Date" 
[1] "2009-09-25" 
[1] "Added Date" 
[1] "2009-10-07"
The same arithmetic can also be performed using the days() function in R, which takes as argument the integer value corresponding to the number of days : 
R
# using required librarieslibrary(lubridate)  # declaring a date objectdate <- "2009-10-01"print ("Original Date")print (date)  # subtracting daysn = 6sub_date <- ymd(date) - days(6)print ("Subtracted Date")print (sub_date)  # adding daysnew_add_date <- ymd(date) + days(0)print ("Added Date")print (new_add_date)
Output:
[1] "Original Date"
[1] "2009-10-01"
[1] "Subtracted Date"
[1] "2009-09-25"
[1] "Added Date" 
[1] "2009-10-01"
Picked
R-DateTime
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
How to filter R dataframe by multiple conditions?
Replace Specific Characters in String in R
Time Series Analysis in R
R - if statement