我想弄清楚如何创建过去 3 年中每个月的第三个星期五的日期向量
提前致谢
最佳答案
RcppBDT包包装了来自 Boost Date_Time 的一些日期(和相关时间)计算.
其中有一个函数在这里做你想做的事:
R> getNthDayOfWeek(third, Fri, Apr, 2014)
[1] "2014-04-18"
R>
代替(包本地)常量 Jan、Feb...,您还可以使用整数来填充过去的第 3-6 个月。
这是一个简单的(丑陋的)序列破解:
R> dates <- data.frame(mon=c(5:12, 1:12, 1:12, 1:4),
+ year=c(rep(2011,8), rep(2012,12),
+ rep(2013,12), rep(2014,4)))
R> sapply(1:36, function(i)
+ format(getNthDayOfWeek(third, Fri, dates[i,1], dates[i,2])))
[1] "2011-05-20" "2011-06-17" "2011-07-15" "2011-08-19"
[5] "2011-09-16" "2011-10-21" "2011-11-18" "2011-12-16"
[9] "2012-01-20" "2012-02-17" "2012-03-16" "2012-04-20"
[13] "2012-05-18" "2012-06-15" "2012-07-20" "2012-08-17"
[17] "2012-09-21" "2012-10-19" "2012-11-16" "2012-12-21"
[21] "2013-01-18" "2013-02-15" "2013-03-15" "2013-04-19"
[25] "2013-05-17" "2013-06-21" "2013-07-19" "2013-08-16"
[29] "2013-09-20" "2013-10-18" "2013-11-15" "2013-12-20"
[33] "2014-01-17" "2014-02-21" "2014-03-21" "2014-04-18"
R>
请注意,我使用了转换为字符的 format()
;否则日期变成数字。不过,这只是 sapply()
的副作用,您可以将 Date
类型分配回我们在此处使用的数据框 dates
。
https://stackoverflow.com/questions/23354069/