Alex Rivera | Logout

Generating a time series with a specific start and end date

Asked 2011-09-29T09:53:38.577
10

I want to generate a time series with all business dates in the range:

startDate = "1990-01-01"
endDate = "1990-12-31"

For example "1990-01-01", "1990-01-02", ...

Edit
Report

2 Answers

14

@csgillespie: chron provides the function is.weekend:

days = seq(as.Date("1990-01-01"), as.Date("1990-12-31"), by="1 day")
library(chron)
weekDays = days[!is.weekend(days)]

## let's check the result with the function weekdays
weekdays(weekDays)

Besides, you can get the same results without chron using format:

isWeekend <- function(x) {format(x, '%w') %in% c(0, 6)}
weekDays2 = days[!isWeekend(days)]
answered 2011-09-29T10:25:53.803
2

There is a base function called ?weekdays.

startDate = "1990-01-01"
endDate = "1990-12-31"

x <- seq(as.Date(startDate), to = as.Date(endDate), by="1 day")

x[!weekdays(x) %in% c("Sunday", "Saturday")]

But, since the actual names of the days will be locale-specific, be sure to set those correctly.

Note that weekdays is just a wrapper on format(x, "%A"). See ?strptime for the details on the format codes.

answered 2011-09-29T11:35:49.633

Your Answer