R – working days in a given week of a month

cal <- data.table(dates = seq.Date(ymd(20200601), ymd(20200731), by = "day")) %>% 
        .[, .(dates, 
              year = year(dates), 
              month = month(dates), 
              week = isoweek(dates),    # new week starts on monday 
              weekday = !(weekdays(dates) %in% c("Sunday", "Saturday"))

cal[, .(work_days = sum(weekday)), by = .(year, month, week)
    ][, week := rowid(month)][]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top