Question: Question:
Is there a function in R that can be used to calculate the number of days excluding weekends and holidays? If you specify "start date" and "end date" as arguments, it is like calculating the number of days in that period. Holiday data is assumed to be prepared manually.
Answer: Answer:
I don't know how the holiday data is stored, but here it is a vector of strings that represent the date.
# 2016年の場合、3月21日が振替休日になっていますが、それは含めていません
> holidays = c("2016/1/1", "2016/1/11", "2016/2/11", "2016/3/20", "2016/4/29",
"2016/5/3", "2016/5/4", "2016/5/5", "2016/7/18", "2016/8/11",
"2016/9/19", "2016/9/22", "2016/10/10", "2016/11/3", "2016/11/23",
"2016/12/23")
# 2016年1月1日から2016年10月23日までのシーケンス(1日単位)を作成
> s = seq.Date(as.Date("2016/1/1"), as.Date("2016/10/23"), by="day")
# format(s, "%u") は曜日を数値で返します。月曜日が 1、日曜日が 7 になります
> length(s[format(s, "%u") < 6 & !format(s, "%Y/%-m/%-d") %in% holidays])
[1] 199