Calculate the number of weekdays between 2 dates in R

Date1 <- as.Date("2011-01-30")
Date2 <- as.Date("2011-02-04")    
sum(!weekdays(seq(Date1, Date2, "days")) %in% c("Saturday", "Sunday"))

EDIT: And Zach said, let there be Vectorize 🙂

Dates1 <- as.Date("2011-01-30") + rep(0, 10)
Dates2 <- as.Date("2011-02-04") + seq(0, 9)
Nweekdays <- Vectorize(function(a, b) 
  sum(!weekdays(seq(a, b, "days")) %in% c("Saturday", "Sunday")))
Nweekdays(Dates1, Dates2)

Leave a Comment