Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 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 object
date <- as.Date("2020/12/11")
print ("Original Date")
print (date)
  
# subtracting 3 days from date 
# object
n = 3
  
# subtracting days 
new_sub_date <- date - n
print ("Subtracted Date")
print (new_sub_date)
  
# adding days
new_add_date <- date + n
print ("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 libraries
library(lubridate)
  
# declaring a date object
date <- "2009-10-01"
print ("Original Date")
print (date)
  
# subtracting 3 days from date
# object
n = 3
  
# subtracting days 
new_sub_date <- date - n
print ("Subtracted Date")
print (new_sub_date)
  
# adding days
new_add_date <- date + n
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-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 libraries
library(lubridate)
  
# declaring a date object
date <- "2009-10-01"
print ("Original Date")
print (date)
  
# subtracting days
n = 6
sub_date <- ymd(date) - days(6)
print ("Subtracted Date")
print (sub_date)
  
# adding days
new_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"



My Personal Notes arrow_drop_up
Recommended Articles
Page :