jmn8 - 1 year ago 71

R Question

I want to create a vector that begins on the most recent weekday date and runs back for 75 days to the weekday 75 weekdays ago - how can I do this? Assume I can use seq? I'm very much a newbie to R.

If today is a Sunday (e.g. 21-08-2016), then the sequence should begin on the prior Friday (19-08-2016) and work back to the weekday 09-05-16. Very much similar to the Excel function =WORKDAY(A1,-1) for example.

Answer Source

Since 75 days is not a large number, it might be something like this:

```
library(chron)
library(lubridate)
n = 75
Filter(function(x) !is.weekend(x), seq.Date(today(), by = '-1 day', length.out = (n+2)*7/5))[1:n]
# [1] "2016-08-19" "2016-08-18" "2016-08-17" "2016-08-16"
# [5] "2016-08-15" "2016-08-12" "2016-08-11" "2016-08-10"
# ...
```