Exclude weekends from dates

Hi, I have a question below:
Create a sequence of class Date that starts from September 1, 2020 and ends on September 30, has all the days, but omits weekend days.
I have all days in September 2020, but do not know how to remove weekends. I just start learning R, can anyone explain how to get that part? Thank you.

dts = seq(as.Date("2000-09-01"),
by = "day",
length.out = 30,
)

It is very clunky but it seems to work

library(tidyverse)

dts = seq(as.Date("2000-09-01"),
by = "day",
length.out = 30,
)

days  <-  weekdays(dts)
dat1  <-  tibble(dts, days)

dat2  <-  with(dat1, subset(dts, days !=  "Saturday"  & days != "Sunday"  ))
```r

The lubridate package has a handy function for this.
By default Saturday is the 6th day of the week, so:

dts[lubridate::wday(dts) < 6]

Edit

Hmm, just double checked this. It seems I remembered wrong - that's what I get for not coding it first! The default first day of the week is actually Sunday (see ?wday), so try this instead

dts[lubridate::wday(dts) %in% 2:6]

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.