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, TRUE) 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 variable? 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]]
On Wed, Nov 19, 2008 at 12: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, > TRUE) > > 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?Have a look at ?"%in%" Hadley -- http://had.co.nz/
chron has some facilities for this that also work with "Date" class: library(chron) 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, > TRUE) > > 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 > variable? > > 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 > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >