Add 1 business day to R

I have an object Datein 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 extended to the next date without vacation. Suppose I mean the NYSE weekend. 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
+4
source share
3 answers

Can I use a combo timeDate::nextBizDay()and roll=-Infto customize your search calendar data.table, for example:

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" 
+7
source

Lubridate will not help you, as this is not a business day concept.

At least two packages, and both of them have a financial bend:

, .

( ) - .

: RQuantLib:

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> 

( dates) .

+6

holidayNYSE(year = getRmetricsOptions("currentYear"))also check isHolidayfrom timeDatepackage

+2
source

All Articles