给定一个随机的开始和结束日期,我如何创建一个新序列(或修改该序列)以在第一个星期一开始并在最后一个星期五结束?
MyDays <- seq(StartDate , EndDate , by = "day")
SequenceDay <- days [!is.weekend(MyDays)]
谢谢!
最佳答案
与Tony Breyal的答案相似。请注意,只有当系统在一周中的几天中使用英语拼写时,这些选项才起作用。
StartDate <- as.Date("2010-01-01")
EndDate <- as.Date("2010-12-31")
myDays <- seq(StartDate , EndDate, by = "day")
excludeDays <- c("Saturday", "Sunday")
myWeekDays <- subset(myDays, !weekdays(myDays) %in% excludeDays)
firstMonday <- which(weekdays(head(myWeekDays, 5)) == "Monday")
lastFriday <- length(myWeekDays) - 5 +
which(weekdays(tail(myWeekDays, 5)) == "Friday")
myCompleteWeeks <- myWeekDays[firstMonday:lastFriday]