提取每个月的第一个星期一 [英] Extract first Monday of every month

查看:150
本文介绍了提取每个月的第一个星期一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从2010-01-01到2015-12-31提取每个月的第一个星期一?

How can I extract the first Monday of every month from 2010-01-01 to 2015-12-31?

推荐答案

我们可以使用 lubridate wday 来测试这是星期一, day 以测试这是否是本月的第一周:

We can use lubridate, wday to test if this is a Monday, and day to test if this is the first week of the month:

library(lubridate)
x <- seq(ymd("2010-01-01"),ymd("2015-12-31"),by="1 day")
x[wday(x,label = TRUE) == "Mon" & day(x) <= 7]

或在base-r(@ DavidArenburg的评论) p>

or in base-r (@DavidArenburg's comment)

x <- seq(as.Date("2010-01-01"), as.Date("2015-12-31"), by = "day")
# You need to adapt "Monday" to the equivalent in your locale
x[weekdays(x) == "Monday" & as.numeric(format(x, "%d")) <= 7]

输出五个第一个结果)

[1] "2010-01-04 UTC" "2010-02-01 UTC" "2010-03-01 UTC" "2010-04-05 UTC" "2010-05-03 UTC" "2010-06-07 UTC"

这篇关于提取每个月的第一个星期一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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