[R] Exclude holidays in a subset of dates?
wuertz at itp.phys.ethz.ch
Wed Nov 19 21:07:37 CET 2008
Gabor Grothendieck wrote:
There are the timeDate and timeSeries packages with very powerful
possibilities to handle weekdays, weekends and public holidays for the
G7 countries and CH. You can create your own holiday calenders for many
countries including fix and moveable holidays.
Note, this allows you to create very simply operators like "add two
business days to a date" taking care of weekends und public as well
ecclestiastical holidays all over the world!
Have look and if you like try it
> chron has some facilities for this that also work with "Date" class:
> startDate <- as.Date("2008-08-15")
> endDate <- as.Date("2008-09-15")
> AllDays <- seq(startDate, endDate, by="day")
> Holidays <- as.chron(as.Date("2008-09-01"))
> is.workday <- !is.holiday(AllDays, Holidays) & !is.weekend(AllDays)
> data.frame(AllDays, is.workday)
> On Wed, Nov 19, 2008 at 1:54 PM, Brigid Mooney <bkmooney at gmail.com> wrote:
>> Hi All,
>> I am iterating through dated materials, with variable start and end dates,
>> and would like to skip procedures everytime I encounter a weekend or
>> holiday. To do this, I thought the easiest way would be to create a
>> TRUE/FALSE vector corresponding to each day where it is TRUE if a workday,
>> and FALSE if a weekend or holiday.
>> So far I have been able to do this for weekdays:
>> startDate <- as.Date("2008-08-15")
>> endDate <- as.Date("2008-09-15")
>> AllDays <- seq(startDate, endDate, by="day")
>> WorkDays <- ifelse(as.numeric(format(startDate+days-1, "%w"))%%6==0, FALSE,
>> But I'm a bit lost as to what to do for the holidays, for example
>> "2008-09-01" is Labor Day in the above range.
>> Is there some procedure to say if an object is "in" a given list or set?
>> Mathematically, I would want to test: day \in Holidays
>> where day is a given day, and Holidays is a set of all Holidays.
>> Is there a way to do this without iteration since my start/endDates are
>> Or maybe there's a very elegant solution that I don't know about as I am
>> still new to R.
>> Thanks for all your help!
>> [[alternative HTML version deleted]]
>> R-help at r-project.org mailing list
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help