Search code examples
rgroup-bybizdaysgapply

Bizdays doesn't exclude weekends


I am trying to calculate utilization rates by relative employee lifespans. I need to assign a total number of hours available to this employee between the earliest and furthest date in which time was recorded. From there I will use this as the divisor in utilization rate = workhours / totalhours.

When testing the bizdays function, I tried a simple example.

bizdays::bizdays("2020-02-07","2020-02-14")
[1] 7

Any reason why the function is not returning the correct number of business days? I am expecting 5 business days since 2/07 was a Friday so only 1 week should be included.

The goals is to use bizdays in the following function that will be applied to a grouped df with gapply.

timeentry = function(x){
  end_date = max(x$terminus)#creates an end_date variable from further end date in the group
  start_date = min(x$onset) #creates a start_date from earliest start date in the group
  start_date %>% bizdays(end_date) * 8 #subtracts dates and multiple by 8 to get work hours between two dates
}

I will apply the function in this manner. Unfortunately, it returns an error suggesting it cannot allocate vector of size 4687 gb. This is a separate issue I hope someone can point out.

util = group %>% gapply(.,timeentry)

where group is the grouped df.


Solution

  • Try setting up your calendar with create.calendar

    library(bizdays)
    create.calendar(name = "demo", weekdays = c("saturday", "sunday"))
    bizdays::bizdays("2020-02-07","2020-02-14", cal = "demo")
    [1] 5