R: search for differences on weekdays

The task of calculating the difference in dates on business days, i.e. exclude weekends, such as the network days function in Excel.

Here are my details.

e <- structure(list(date.pr = structure(c(15909, 15933, 16517, 15961, 15974, 15978), class = "Date"), date.po = structure(c(15909, 15933, 15954, 15961, 15974, 15978), class = "Date")), .Names = c("date.1", "date.2"), class = c("tbl_df", "data.frame"), row.names = c(NA, -6L)) 

Found the bizdays package for this task. Which is great for this.

 > bizdays(e$date.2,e$date.1) [1] 0 0 563 0 0 0 

But my data contains cases where date 2 is before date .1.

 e2 <- structure(list(date.pr = structure(c(15909, 15933, 16517, 15961, 5974, 15978, 15978), class = "Date"), date.po = structure(c(15909, 15933, 15954, 15961, 15974, 15978, 15979), class = "Date")), .Names = c("date.1", "date.2"), class = c("tbl_df", "data.frame"), row.names = c(NA, -7L)) 

Now it gives the following error:

 > cal <- Calendar(holidaysANBIMA, weekdays=c("saturday","sunday")) > bizdays(e2$date.2,e2$date.1,cal) Error in bizdays.Date(e2$date.2, e2$date.1, cal) : All from dates must be greater than all to dates. 

I think using ifelse () logic, but it gives me the same error.

 > ifelse(e2$date.2 < e2$date.1, NA, bizdays(e2$date.2,e2$date.1,cal)) Error in bizdays.Date(e2$date.2, e2$date.1, cal) : All from dates must be greater than all to dates. 

Help evaluate.

+1
date r
source share
1 answer

Nweekdays() function adapted from @J. I won. Decision on Calculate the number of days of the week between two dates in R

This modified function takes into account date differences, both positive and negative, while the above link made a decision for a positive date difference.

 library("dplyr") e2 <- structure(list(date.pr = structure(c(16524, 16524, 16507, 16510, 16510, 16524, 16510, 5974), class = "Date"), date.po = structure(c(16524, 16525, 16510, 16517, 16524, 16510, 16531, 15974), class = "Date")), .Names = c("date.1", "date.2"), class = c("tbl_df", "data.frame"), row.names = c(NA, -8L)) 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)) }) > 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 [8 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-13 2015-03-16 Friday Monday 3 1 4 2015-03-16 2015-03-23 Monday Monday 7 5 5 2015-03-16 2015-03-30 Monday Monday 14 10 6 2015-03-30 2015-03-16 Monday Monday 14 10 7 2015-03-16 2015-04-06 Monday Monday 21 15 8 1986-05-11 2013-09-26 Sunday Thursday 10000 7143 > e2 %>% mutate(ndays_no_wkends = Nweekdays(date.1, date.2)) Source: local data frame [8 x 3] date.1 date.2 ndays_no_wkends (date) (date) (dbl) 1 2015-03-30 2015-03-30 0 2 2015-03-30 2015-03-31 1 3 2015-03-13 2015-03-16 1 4 2015-03-16 2015-03-23 5 5 2015-03-16 2015-03-30 10 6 2015-03-30 2015-03-16 10 7 2015-03-16 2015-04-06 15 8 1986-05-11 2013-09-26 7143 
+1
source share

All Articles