Clayton Clayton - 27 days ago 5
R Question

Using timeDate holidays on a list

I recently asked a question about getting a sequence of dates into a list: Create a list of time sequences from dates between 2 vectors.

The last line of the code below answered my question:

library(timeDate)
Dates <- data.frame(Start = c("2014-05-09 10:48:00",
"2014-05-11 19:39:00",
"2014-10-09 08:52:00",
"2014-10-29 10:48:59",
"2014-11-13 03:08:00"),
End = c("2014-05-11 19:39:00",
"2014-05-12 07:05:00",
"2014-10-29 10:48:59",
"2014-11-13 03:08:00",
"2014-11-13 08:41:59"))
Date_List <- mapply(timeSequence, Dates$Start, Dates$End, by = "min")


However, I need to only include working days so I use a
timeDate
function again:

library(lubridate)
Holidays <- c("2013-07-04", "2013-09-02", "2013-10-14", "2013-11-11", "2013-11-21", "2013-11-22", as.character(seq(ymd("2013-12-24"), ymd("2014-01-01"), "days")),
"2014-01-20", "2014-02-17", "2014-05-26", "2014-07-04", "2014-09-01", "2014-10-13", "2014-11-11", "2014-11-27", "2014-11-28", as.character(seq(ymd("2014-12-24"), ymd("2015-01-01"), "days")),
"2015-01-19", "2015-02-16", "2015-05-25", "2015-07-03", "2015-09-07", "2015-10-12", "2015-11-11", "2015-11-26", "2015-11-27", as.character(seq(ymd("2015-12-24"), ymd("2016-01-01"), "days")),
"2016-01-18", "2016-02-15", "2016-05-30", "2016-07-04", "2016-09-05", "2016-10-10", "2016-11-11", "2016-11-24", "2016-11-25", as.character(seq(ymd("2016-12-24"), ymd("2017-01-01"), "days")),
"2017-01-16", "2017-02-20", "2017-05-29", "2017-07-04", "2017-09-04", "2017-10-09", "2017-11-10", "2017-11-23", "2017-11-24", as.character(seq(ymd("2016-12-24"), ymd("2017-01-01"), "days")))
Holidays <- timeDate(as.Date(Holidays))

for (b in 1:length(Date_List)) {
Biz_Seq[[b]] <- isBizday(Date_List[[b]], Holidays)
}


This adds whether or not each part of the sequence is in a working day or not. I want it to return the sequences in the list WITHOUT any of the non-working day parts (i.e., remove the parts of the sequence that are
FALSE
).

I have not found anything on SO with this particular issue in subsetting a list. Thank you in advance.

Answer

I think you're not trying to subset a list but rather the vectors of class timeDate in the elements of that list. This could simply be done with:

Biz_Seq <- lapply(Date_List, function(days) days[isBizday(days, Holidays)])