Adding 15 business days in lubridate

前端 未结 5 1595
别那么骄傲
别那么骄傲 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:03

    There's a nifty function isBizday in the timeDate package that made this more fun than it seemed on first glance.

    date.in <- dmy(c("30-8-2001", "12-1-2003", "28-2-2003", "20-5-2004"))
    

    Here's a function to do the work. It seemed reasonable to choose 1:10 for the days to look ahead, but that can be adjusted of course.

    deadline <- function(x) {
        days <- x + 1:10
        Deadline <- days[isBizday(as.timeDate(days))][6]
        data.frame(DateIn = x, Deadline, DayOfWeek = weekdays(Deadline), 
                   TimeDiff = difftime(Deadline, x))
    }
    

    And here's the result:

    library(timeDate)
    Reduce(rbind, Map(deadline, as.Date(date.in)))
    #       DateIn   Deadline DayOfWeek TimeDiff
    # 1 2001-08-30 2001-09-07    Friday   8 days
    # 2 2003-01-12 2003-01-20    Monday   8 days
    # 3 2003-02-28 2003-03-10    Monday  10 days
    # 4 2004-05-20 2004-05-28    Friday   8 days
    

提交回复
热议问题