Open In App

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

Last Updated : 25 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Managing dates and times is a crucial aspect of data analysis, and R provides robust facilities for dealing with date objects. In this post, we will look at how to add and remove days from a date in R. Whether you’re dealing with time series data, need to compute future dates, or want to browse through historical records, R makes date manipulation simple.

Dates in R

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.

Add and Subtract Days from Date 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 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)

First, Let’s create one date variable.

R




date <- '2023-07-31'
print(date)


Output:

[1] "2023-07-31"

Now check the class of our date variable.

R




class(date)


Output:

[1] "character"

Now we will use as.Date method and convert the class of our date variable from character to date.

R




date <- as.Date(date)
 
class(date)


Output:

[1] "Date"

Adding a number of days in date.

R




date + 10


Output:

[1] "2023-08-10"

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"

Add and Subtract Days from Date using 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 

R




# using required libraries
library(lubridate)
 
# declaring a date object
date <- "2009-10-01"
print ("Original Date")
print (date)
 
# Convert date to Date object using lubridate
date <- ymd(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-28" 
[1] "Added Date" 
[1] "2009-10-04"

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"


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads