Calculate the number of weekdays between 2 dates in R

前端 未结 6 1026
北海茫月
北海茫月 2020-11-29 02:59

I\'m trying to write an R function to calculate the number of weekdays between two dates. For example, Nweekdays(\'01/30/2011\',\'02/04/2011\') would equal 5.

Simila

6条回答
  •  感动是毒
    2020-11-29 03:52

    Working this out using lubridate you can create a function like:

    library(lubridate)
    
    WorkingDays_function <- function(StartDate,EndDate){
    startDate <- dmy(StartDate)
    endDate <- dmy(EndDate)
    
    #Now build a sequence between the dates:
    myDates <-seq(from = startDate, to = endDate, by = "days")
    
    
    #Week starts on Sunday (1) so to exclude Sun (1) and Sat (7)
    #use > 1 & < 7  
    working_days <- sum(wday(myDates)>1 & wday(myDates)<7)
    
    print(working_days)
    }
    
    WorkingDays_function("11/07/2019","20/07/2019") 
    

提交回复
热议问题