简体   繁体   English

在列表上使用timeDate假期

[英]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 . 我最近问了一个有关将日期序列放入列表的问题: 从2个向量之间的日期创建一个时间序列列表

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: 但是,我只需要包含工作日,因此我可以再次使用timeDate函数:

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 (ie, remove the parts of the sequence that are FALSE ). 我希望它返回列表中的序列,而没有任何非工作日部分(即,删除序列中为FALSE )。

I have not found anything on SO with this particular issue in subsetting a list. 在子集列表中,我没有发现任何关于此特定问题的信息。 Thank you in advance. 先感谢您。

I think you're not trying to subset a list but rather the vectors of class timeDate in the elements of that list. 我认为您不是要对列表进行子集化,而是timeDate在该列表的元素中添加timeDate类的向量 This could simply be done with: 这可以简单地通过以下方式完成:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM