仅在工作日填写一个向量 [英] Fill a vector with weekdays only

查看:186
本文介绍了仅在工作日填写一个向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道开始日期开始和最后日期成熟度。如何填写带有日期的向量,而不考虑周末日期?
例如,我们假设:

I know the start date start and the last date maturity. How can I fill in a vector with dates without taking weekends dates into account ? For instance, let's say :

> start = as.Date("2013-02-28");
> maturity = as.Date("2013-03-07");

我想获得以下矢量:

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"

相同的结果...?

推荐答案

从多个软件包可以使用各种功能来执行此操作。但我的第一个想法是简单地做一个序列,然后删除周末:

There are probably a billion ways to do this with a variety of functions from multiple packages. But my first thought is to simply make a sequence and then remove the weekends:

x <- seq(as.Date('2011-01-01'),as.Date('2011-12-31'),by = 1)
x <- x[!weekdays(x) %in% c('Saturday','Sunday')]

此答案仅适用于基于英文的系统。例如,在法语版本中,'星期六'和'星期日'必须翻译成'samedi'和'dimanche'

This answer is valid only with an English based system. For instance, in a French version, 'Saturday' and 'Sunday' must be translated into 'samedi' and 'dimanche'

这篇关于仅在工作日填写一个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆