Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the number of weekdays between 2 dates in R

Tags:

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.

Similar to this question. Thanks!

/edit: @J. Winchester's answer is great, but I was wondering if anyone could think of a way to vectorize this, so that it'll work on 2 columns of dates. Thanks! /edit 2: Thanks again!

like image 783
Zach Avatar asked Feb 18 '11 21:02

Zach


People also ask

How are working days calculated in R?

Count business days in R Calculating the number of business days can be done using the (identically named) bizdays function. It takes only three arguments that you'll need to achieve your goals: (1) the start date, (2) the end data and (3) a calendar object. A calendar object can created using the create.

How do I get weekdays in R?

Determine the Weekday on a Specific Date in R Programming – weekdays() Function. weekdays() function in R Language is used to determine the weekday on a specific date passed to it as argument.


2 Answers

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)
like image 103
J. Win. Avatar answered Sep 18 '22 18:09

J. Win.


These modified functions takes into account of date differences of either positive or negative, whereas the accepted solution accounts for positive date difference.

library("dplyr")

e2 <- structure(list(date.pr = structure(c(16524, 16524, 16524, 16524, 16524, 16524, 16524, 16524, 16524, 16524, 16545, 5974), class = "Date"), 
                     date.po = structure(c(16524, 16525, 16526, 16527, 16528, 16529, 16530, 16531, 16538, 16545, 16524, 15974), class = "Date")), 
                .Names = c("date.1", "date.2"), class = c("tbl_df", "data.frame"), row.names = c(NA, -12L))

1. Locale Dependent Solution: Nweekdays() function is adapted from @J. Won.'s solution. It works for locale = "English_United States.1252"

Nweekdays <- Vectorize(
  function(a, b) 
  {
    ifelse(a < b, 
           return(sum(!weekdays(seq(a, b, "days")) %in% c("Saturday", "Sunday")) - 1), 
           return(sum(!weekdays(seq(b, a, "days")) %in% c("Saturday", "Sunday")) - 1))
  })

a. English Locale

> Sys.setlocale(category="LC_ALL", locale = "English_United States.1252")
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"

> Sys.getlocale()
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"

> e2 %>%
    mutate(wkd1 = format(date.1, "%A"),
           wkd2 = format(date.2, "%A"),
           ndays_with_wkends = ifelse((date.2 > date.1), (date.2 - date.1), (date.1 - date.2)), 
           ndays_no_wkends = Nweekdays(date.1, date.2))

Source: local data frame [12 x 6]

       date.1     date.2   wkd1      wkd2 ndays_with_wkends ndays_no_wkends
       (date)     (date)  (chr)     (chr)             (dbl)           (dbl)
1  2015-03-30 2015-03-30 Monday    Monday                 0               0
2  2015-03-30 2015-03-31 Monday   Tuesday                 1               1
3  2015-03-30 2015-04-01 Monday Wednesday                 2               2
4  2015-03-30 2015-04-02 Monday  Thursday                 3               3
5  2015-03-30 2015-04-03 Monday    Friday                 4               4
6  2015-03-30 2015-04-04 Monday  Saturday                 5               4
7  2015-03-30 2015-04-05 Monday    Sunday                 6               4
8  2015-03-30 2015-04-06 Monday    Monday                 7               5
9  2015-03-30 2015-04-13 Monday    Monday                14              10
10 2015-03-30 2015-04-20 Monday    Monday                21              15
11 2015-04-20 2015-03-30 Monday    Monday                21              15
12 1986-05-11 2013-09-26 Sunday  Thursday             10000            7143

b. Chinese Locale

> Sys.setlocale(category="LC_ALL", locale = "chinese")
[1] "LC_COLLATE=Chinese (Simplified)_People's Republic of China.936;LC_CTYPE=Chinese (Simplified)_People's Republic of China.936;LC_MONETARY=Chinese (Simplified)_People's Republic of China.936;LC_NUMERIC=C;LC_TIME=Chinese (Simplified)_People's Republic of China.936"

> Sys.getlocale()
[1] "LC_COLLATE=Chinese (Simplified)_People's Republic of China.936;LC_CTYPE=Chinese (Simplified)_People's Republic of China.936;LC_MONETARY=Chinese (Simplified)_People's Republic of China.936;LC_NUMERIC=C;LC_TIME=Chinese (Simplified)_People's Republic of China.936"

> e2 %>%
    mutate(wkd1 = format(date.1, "%A"),
           wkd2 = format(date.2, "%A"),
           ndays_with_wkends = ifelse((date.2 > date.1), (date.2 - date.1), (date.1 - date.2)), 
           ndays_no_wkends = Nweekdays(date.1, date.2))

Source: local data frame [12 x 6]

       date.1     date.2   wkd1   wkd2 ndays_with_wkends ndays_no_wkends
       (date)     (date)  (chr)  (chr)             (dbl)           (dbl)
1  2015-03-30 2015-03-30 ÐÇÆÚÒ» ÐÇÆÚÒ»                 0               0
2  2015-03-30 2015-03-31 ÐÇÆÚÒ» ÐÇÆÚ¶þ                 1               1
3  2015-03-30 2015-04-01 ÐÇÆÚÒ» ÐÇÆÚÈý                 2               2
4  2015-03-30 2015-04-02 ÐÇÆÚÒ» ÐÇÆÚËÄ                 3               3
5  2015-03-30 2015-04-03 ÐÇÆÚÒ» ÐÇÆÚÎå                 4               4
6  2015-03-30 2015-04-04 ÐÇÆÚÒ» ÐÇÆÚÁù                 5               5
7  2015-03-30 2015-04-05 ÐÇÆÚÒ» ÐÇÆÚÈÕ                 6               6
8  2015-03-30 2015-04-06 ÐÇÆÚÒ» ÐÇÆÚÒ»                 7               7
9  2015-03-30 2015-04-13 ÐÇÆÚÒ» ÐÇÆÚÒ»                14              14
10 2015-03-30 2015-04-20 ÐÇÆÚÒ» ÐÇÆÚÒ»                21              21
11 2015-04-20 2015-03-30 ÐÇÆÚÒ» ÐÇÆÚÒ»                21              21
12 1986-05-11 2013-09-26 ÐÇÆÚÈÕ ÐÇÆÚËÄ             10000           10000

2. Locale Independent Solution: Nweekdays() function is adapted from @Sacha Epskamp's solution. It works for all locales, however @Sacha Epskamp used c(0,6) to weed out the weekends, which is different from this solution which uses c(2,3) to extract out weekends.

Nweekdays <- Vectorize(
  function(a, b) {
           return(sum(!(((as.numeric(b:a)) %% 7) %in% c(2,3))) - 1) # 2: Saturday and 3: Sunday
  })

a. English Locale

> Sys.setlocale(category="LC_ALL", locale = "English_United States.1252")
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"

> Sys.getlocale()
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"

> e2 %>%
    mutate(wkd1 = format(date.1, "%A"),
           wkd2 = format(date.2, "%A"),
           ndays_with_wkends = ifelse((date.2 > date.1), (date.2 - date.1), (date.1 - date.2)), 
           ndays_no_wkends = Nweekdays(date.1, date.2))

Source: local data frame [12 x 6]

       date.1     date.2   wkd1      wkd2 ndays_with_wkends ndays_no_wkends
       (date)     (date)  (chr)     (chr)             (dbl)           (dbl)
1  2015-03-30 2015-03-30 Monday    Monday                 0               0
2  2015-03-30 2015-03-31 Monday   Tuesday                 1               1
3  2015-03-30 2015-04-01 Monday Wednesday                 2               2
4  2015-03-30 2015-04-02 Monday  Thursday                 3               3
5  2015-03-30 2015-04-03 Monday    Friday                 4               4
6  2015-03-30 2015-04-04 Monday  Saturday                 5               4
7  2015-03-30 2015-04-05 Monday    Sunday                 6               4
8  2015-03-30 2015-04-06 Monday    Monday                 7               5
9  2015-03-30 2015-04-13 Monday    Monday                14              10
10 2015-03-30 2015-04-20 Monday    Monday                21              15
11 2015-04-20 2015-03-30 Monday    Monday                21              15
12 1986-05-11 2013-09-26 Sunday  Thursday             10000            7143

b. Chinese Locale

> Sys.setlocale(category="LC_ALL", locale = "chinese")
[1] "LC_COLLATE=Chinese (Simplified)_People's Republic of China.936;LC_CTYPE=Chinese (Simplified)_People's Republic of China.936;LC_MONETARY=Chinese (Simplified)_People's Republic of China.936;LC_NUMERIC=C;LC_TIME=Chinese (Simplified)_People's Republic of China.936"

> Sys.getlocale()
[1] "LC_COLLATE=Chinese (Simplified)_People's Republic of China.936;LC_CTYPE=Chinese (Simplified)_People's Republic of China.936;LC_MONETARY=Chinese (Simplified)_People's Republic of China.936;LC_NUMERIC=C;LC_TIME=Chinese (Simplified)_People's Republic of China.936"

> e2 %>%
    mutate(wkd1 = format(date.1, "%A"),
           wkd2 = format(date.2, "%A"),
           ndays_with_wkends = ifelse((date.2 > date.1), (date.2 - date.1), (date.1 - date.2)), 
           ndays_no_wkends = Nweekdays(date.1, date.2))

Source: local data frame [12 x 6]

       date.1     date.2   wkd1   wkd2 ndays_with_wkends ndays_no_wkends
       (date)     (date)  (chr)  (chr)             (dbl)           (dbl)
1  2015-03-30 2015-03-30 ÐÇÆÚÒ» ÐÇÆÚÒ»                 0               0
2  2015-03-30 2015-03-31 ÐÇÆÚÒ» ÐÇÆÚ¶þ                 1               1
3  2015-03-30 2015-04-01 ÐÇÆÚÒ» ÐÇÆÚÈý                 2               2
4  2015-03-30 2015-04-02 ÐÇÆÚÒ» ÐÇÆÚËÄ                 3               3
5  2015-03-30 2015-04-03 ÐÇÆÚÒ» ÐÇÆÚÎå                 4               4
6  2015-03-30 2015-04-04 ÐÇÆÚÒ» ÐÇÆÚÁù                 5               4
7  2015-03-30 2015-04-05 ÐÇÆÚÒ» ÐÇÆÚÈÕ                 6               4
8  2015-03-30 2015-04-06 ÐÇÆÚÒ» ÐÇÆÚÒ»                 7               5
9  2015-03-30 2015-04-13 ÐÇÆÚÒ» ÐÇÆÚÒ»                14              10
10 2015-03-30 2015-04-20 ÐÇÆÚÒ» ÐÇÆÚÒ»                21              15
11 2015-04-20 2015-03-30 ÐÇÆÚÒ» ÐÇÆÚÒ»                21              15
12 1986-05-11 2013-09-26 ÐÇÆÚÈÕ ÐÇÆÚËÄ             10000            7143
like image 42
Sathish Avatar answered Sep 19 '22 18:09

Sathish