I want to calculate number of days between two dates by excluding the weekends. enter image description here
Asked
Active
Viewed 1,347 times
-4
-
Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Aug 29 '18 at 10:51
-
2Possible duplicate of [Calculate the number of weekdays between 2 dates in R](https://stackoverflow.com/questions/5046708/calculate-the-number-of-weekdays-between-2-dates-in-r) – phiver Aug 29 '18 at 11:27
2 Answers
1
You can make a sequence of dates (and days, both with lubrirdate
), filter out the weekends and count the number of rows:
library(dplyr)
library(lubridate)
df <- data_frame(date = seq(ymd("2018-06-01"), ymd("2018-09-30"), by = "days"))
days <- mutate(df, day = wday(date, label = T)) %>%
filter(day != "Sat", day != "Sun") %>%
nrow()

Paul
- 2,877
- 1
- 12
- 28
-
-
Can you provide more information as to what you want, with a reproducible example, what you have tried and what you expect to happen? – Paul Aug 31 '18 at 12:06
-
Hi Paul, thanks for the help i actually figured out a solution using 'bizdays' package this helped me in excluding the weekends. – Shravan Shetty Sep 03 '18 at 09:32
0
You can do it with help of library chron Sample Code:
library(chron)
number_of_days <- (START_DATE,END_DATE,by=1)
length(number_of_days)
length(number_of_days[!is.weekend(number_of_days)])
Hope this helps

Hunaidkhan
- 1,411
- 2
- 11
- 21