在计算工作日的日期差异时遇到问题,即不包括周末,如Excel中的networkday函数。
这是我的数据。
e <- structure(list(date.pr = structure(c(15909, 15933, 16517, 15961, 15974, 15978), class = "Date"), date.po = structure(c(15909, 15933, 15954, 15961, 15974, 15978), class = "Date")), .Names = c("date.1", "date.2"), class = c("tbl_df", "data.frame"), row.names = c(NA, -6L))找到此任务的“bizday”包。这对这一个很好用。
> bizdays(e$date.2,e$date.1)
[1] 0 0 563 0 0 0但是我的数据包含日期2在日期1之前的情况。
e2 <- structure(list(date.pr = structure(c(15909, 15933, 16517, 15961, 5974, 15978, 15978), class = "Date"), date.po = structure(c(15909, 15933, 15954, 15961, 15974, 15978, 15979), class = "Date")), .Names = c("date.1", "date.2"), class = c("tbl_df", "data.frame"), row.names = c(NA, -7L))现在,它给出了以下错误:
> cal <- Calendar(holidaysANBIMA, weekdays=c("saturday","sunday"))
> bizdays(e2$date.2,e2$date.1,cal)
Error in bizdays.Date(e2$date.2, e2$date.1, cal) :
All from dates must be greater than all to dates.我在考虑使用ifelse()逻辑,但它给出了相同的错误。
> ifelse(e2$date.2 < e2$date.1, NA, bizdays(e2$date.2,e2$date.1,cal))
Error in bizdays.Date(e2$date.2, e2$date.1, cal) :
All from dates must be greater than all to dates.感谢你的帮助。
https://stackoverflow.com/questions/38448310
复制相似问题