我知道开始日期start和最后日期maturity.如何在不考虑周末日期的情况下填写带日期的向量?例如,让我们说:
> start = as.Date("2013-02-28");
> maturity = as.Date("2013-03-07");
Run Code Online (Sandbox Code Playgroud)
我希望得到以下向量:
results
[1] "2013-03-01" "2013-03-04" "2013-03-05" "2013-03-06" "2013-03-07"
> start = as.Date("2013-02-28");
> maturity = as.Date("2013-03-07");
> x <- seq(start,maturity,by = 1)
> x
[1] "2013-02-28" "2013-03-01" "2013-03-02" "2013-03-03" "2013-03-04" "2013-03-05"
[7] "2013-03-06" "2013-03-07"
> x <- x[!weekdays(x) %in% c('Saturday','Sunday')]
> x
[1] "2013-02-28" "2013-03-01" "2013-03-02" "2013-03-03" "2013-03-04" "2013-03-05"
[7] "2013-03-06" "2013-03-07"
Run Code Online (Sandbox Code Playgroud)
结果相同......?
使用多个包中的各种功能可能有十亿种方法可以做到这一点.但我的第一个想法是简单地制作一个序列,然后删除周末:
x <- seq(as.Date('2011-01-01'),as.Date('2011-12-31'),by = 1)
x <- x[!weekdays(x) %in% c('Saturday','Sunday')]
Run Code Online (Sandbox Code Playgroud)
此答案仅适用于基于英语的系统.例如,在法语版本中,"星期六"和"星期日"必须翻译成"samedi"和"dimanche"