How to get the date of the last business day of the week in R?

272 Views Asked by At

I want to know if today is the last business day of this week in R. I was thinking of using getdate() from Bizdays library, so I created the calendar inputting the holidays in my country. however, in the next step of actually using getdate() function, it only accepts "year" and "month" as reference, not "week".

library(bizdays)
create.calendar(name='Indonesia', holidays = tbl_Libur$Tanggal,
       weekdays=c('sunday', 'saturday'), financial = FALSE)
date.now <- Sys.Date()
    
getdate("last bizday", ref(date.now, "week"), "Indonesia")

the result of the code above is

Error in match.arg(ym) : 'arg' should be one of “month”, “year”

Is there a better/simpler way of doing it, or I will have to brute force evaluating each day of the week using is.bizday() and get the last true value like this?

weekStart <- date.now - wday(date.now) + 1

for(i in 0:6){
  tgl <- weekStart + i
  if(is.bizday(tgl, "Indonesia")){
    result <- tgl  
  }
}
result

Thanks

0

There are 0 best solutions below