Adding 15 business days in lubridate

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

    The package bizdays has the function offset which offsets the given dates by a number of business days. It relies on the calendar you define and of course you can define a calendar where weekends are the only nonworking days.

    Here is an example:

    library(lubridate)
    library(bizdays)
    cal <- Calendar(weekdays=c('saturday', 'sunday'))
    date.in <- dmy(c("30-8-2001", "12-1-2003", "28-2-2003", "20-5-2004"))
    bizdays::offset(date.in, 6, cal)
    
    # [1] "2001-09-07" "2003-01-21" "2003-03-10" "2004-05-28"
    

    2018 Update

    The function Calendar in bizdays has been renamed to create.calendar, but (in April 2018) a warning is no longer issued.

    The code should now be slightly different:

    library(lubridate)
    library(bizdays)
    create.calendar(name="mycal", weekdays=c('saturday', 'sunday'))
    date.in <- dmy(c("30-8-2001", "12-1-2003", "28-2-2003", "20-5-2004"))
    bizdays::offset(date.in, 6, "mycal")
    
    # [1] "2001-09-07" "2003-01-21" "2003-03-10" "2004-05-28"
    

提交回复
热议问题