Adding 15 business days in lubridate

前端 未结 5 1605
别那么骄傲
别那么骄傲 2020-12-10 06:32

I have a long list of start dates of a certain procedure. Rules require the procedure to be completed in, at most, 6 business days. I wish to compute the deadline.

5条回答
  •  盖世英雄少女心
    2020-12-10 07:02

    Here is the @richard-craven solution --- it takes holidays other than weekends into account, which is a plus --- generalized to a variable number of business days.

    library(lubridate)
    library(timeDate)
    bizDeadline <- function(x, nBizDys = 6){
        output <- Reduce(rbind, Map((function(x, howMuch = 15){
            x <- as.Date(x)
            days <- x + 1:(howMuch*2)
            Deadline <- days[isBizday(as.timeDate(days))][howMuch]
            data.frame(DateIn = x, Deadline, DayOfWeek = weekdays(Deadline),   
                       TimeDiff = difftime(Deadline, x))  # useful to get more info, if so wished
        }), x, howMuch = nBizDys))
        output$Deadline
    }
    # example 
    date.in <- dmy(c("30-8-2001", "12-1-2003", "28-2-2003", "20-5-2004"))
    bizDeadline(date.in, nBizDys=31)
    # [1] "2001-10-12" "2003-02-24" "2003-04-14" "2004-07-02"
    

    (Interesting extension: How do you change default=holidayNYSE with non-prepackaged holidays in package timeDate (eg., Chile's http://www.feriadoschilenos.cl/)? But that is another question.)

    Thanks for your help!

提交回复
热议问题