jmn8 jmn8 - 1 year ago 62
R Question

How can I create a Vector of weekdays from "Today" to a date 75 days ago in R?

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:

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"
 # ...