Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add 1 business day to date in R

Tags:

date

r

lubridate

I have a Date object in R and would like to add 1 business day to this date. If the result is a holiday, I would like the date to be incremented to the next non-holiday date. Let's assume I mean NYSE holidays. How can I do this?

Example:

mydate = as.Date("2013-12-24")
mydate + 1 #this is a holiday so I want this to roll over to the 26th instead
like image 468
Alex Avatar asked Dec 20 '13 18:12

Alex


People also ask

How do I add business days in R?

Add or subtract business days in RIt contains an offset() function to add or subtract business days from a specific date. Here, too, you need to specify the calendar object you'd like to use. A good tip is to specify the library with the function, because offset is a fairly used function name in other libraries.

How do you add days in R?

Method 2: lubridate package in R 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.

How do I get the first day of the current month in R?

The first date of the month in R You can do it with the floor_date function from lubridate. If you have the first date of the month, then it is easy to calculate the last day of the previous month by simple subtraction, but it will work with the class of Date.


3 Answers

I might use a combo of timeDate::nextBizDay() and roll=-Inf to set up a data.table lookup calendar, like this:

library(data.table)
library(timeDate)

## Set up a calendar for 2013 & 2014
cal <- data.table(date=seq(from=as.Date("2013-01-01"), by=1, length=730),
                  key="date")    
cal2 <- copy(cal)
cal2[,nextBizDay:=date+1]
cal2 <- cal2[isBizday(as.timeDate(nextBizDay)),]
cal <- cal2[cal,,roll=-Inf]

## Check that it works
x <- as.Date("2013-12-21")+1:10
cal[J(x),]
#           date nextBizDay
#  1: 2013-12-22 2013-12-23
#  2: 2013-12-23 2013-12-24
#  3: 2013-12-24 2013-12-26
#  4: 2013-12-25 2013-12-26
#  5: 2013-12-26 2013-12-27
#  6: 2013-12-27 2013-12-30
#  7: 2013-12-28 2013-12-30
#  8: 2013-12-29 2013-12-30
#  9: 2013-12-30 2013-12-31
# 10: 2013-12-31 2014-01-01

## Or perhaps:

lu <- with(cal, setNames(nextBizDay, date))
lu[as.character(x[1:6])]
#   2013-12-22   2013-12-23   2013-12-24   2013-12-25   2013-12-26   2013-12-27 
# "2013-12-23" "2013-12-24" "2013-12-26" "2013-12-26" "2013-12-27" "2013-12-30" 
like image 161
Josh O'Brien Avatar answered Oct 20 '22 07:10

Josh O'Brien


Lubridate will not help you as it does not a notion of business days.

At least two packages do, and they both have a financial bent:

  • RQuantLib has exchange calendars for many exchanges (but it is a pretty large package)

  • timeDate also has calendars

Both packages have decent documentation which will permit you to set this up from working examples.

A third option (for simple uses) is to just store a local calendar out a few years and use that.

Edit: Here is a quick RQuantLib example:

R> library(RQuantLib)
R> adjust(calendar="TARGET", dates=Sys.Date()+2:6, bdc = 0)
  2013-12-22   2013-12-23   2013-12-24   2013-12-25   2013-12-26 
"2013-12-23" "2013-12-23" "2013-12-24" "2013-12-27" "2013-12-27" 
R> 

It just moves the given day (from argument dates) forward to the next biz day.

like image 24
Dirk Eddelbuettel Avatar answered Oct 20 '22 08:10

Dirk Eddelbuettel


holidayNYSE(year = getRmetricsOptions("currentYear")) also check out isHoliday from timeDate package

like image 41
Prasanna Nandakumar Avatar answered Oct 20 '22 07:10

Prasanna Nandakumar