How can I extract the first Monday of every month from 2010-01-01 to 2015-12-31?
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]
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]
output (five first results)
[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"
x <- seq(as.Date("2010-01-01"), as.Date("2015-12-31"), by = "day") ; x[weekdays(x) == "Monday" & as.numeric(format(x, "%d")) <= 7]
with base R –
Barrera Another apprach: using the Boost Date_Time library:
library(RcppBDT)
dates <- seq(as.Date("2010-01-01"), as.Date("2015-12-31"), by="1 month")
do.call(c, lapply(dates-1, getFirstDayOfWeekAfter, dow=Mon))
# [1] "2010-01-04" "2010-02-01" "2010-03-01" "2010-04-05" "2010-05-03"...
Here is a base R solution. It only requires three lines of code. In the zoo quickref vignette there is a one line function, nextfri
, to get the next Friday on or after a given date. If we replace each 5 (Friday) with a 1 (Monday) in the body of that function we get the next Monday on or after a given date. (The formula there does not include the origin
argument that is used here since zoo allows it to be omitted; however, here we include it so that it works without any packages.)
# given "Date" class vector x return same date if Mon or next Mon if not
nextmon <- function(x) 7 * ceiling(as.numeric(x-1+4)/7) + as.Date(1-4, origin="1970-01-01")
# all first of months between the indicated dates
firsts <- seq(as.Date("2010-01-01"), as.Date("2015-12-31"), "month")
# first Monday in each month
nextmon(firsts)
`giving:
[1] "2010-01-04" "2010-02-01" "2010-03-01" "2010-04-05" "2010-05-03"
[6] "2010-06-07" "2010-07-05" "2010-08-02" "2010-09-06" "2010-10-04"
[11] "2010-11-01" "2010-12-06" "2011-01-03" "2011-02-07" "2011-03-07"
[16] "2011-04-04" "2011-05-02" "2011-06-06" "2011-07-04" "2011-08-01"
[21] "2011-09-05" "2011-10-03" "2011-11-07" "2011-12-05" "2012-01-02"
[26] "2012-02-06" "2012-03-05" "2012-04-02" "2012-05-07" "2012-06-04"
[31] "2012-07-02" "2012-08-06" "2012-09-03" "2012-10-01" "2012-11-05"
[36] "2012-12-03" "2013-01-07" "2013-02-04" "2013-03-04" "2013-04-01"
[41] "2013-05-06" "2013-06-03" "2013-07-01" "2013-08-05" "2013-09-02"
[46] "2013-10-07" "2013-11-04" "2013-12-02" "2014-01-06" "2014-02-03"
[51] "2014-03-03" "2014-04-07" "2014-05-05" "2014-06-02" "2014-07-07"
[56] "2014-08-04" "2014-09-01" "2014-10-06" "2014-11-03" "2014-12-01"
[61] "2015-01-05" "2015-02-02" "2015-03-02" "2015-04-06" "2015-05-04"
[66] "2015-06-01" "2015-07-06" "2015-08-03" "2015-09-07" "2015-10-05"
[71] "2015-11-02" "2015-12-07"
This single line code will give the desired 72 Mondays:
sort(format(as.Date(Reduce(paste0,expand.grid(2010:2015,-1:-12,-1:-7))),"%A, %Y-%m-%d"))[73:144]
[1] "Monday, 2010-01-04" "Monday, 2010-02-01" "Monday, 2010-03-01"
[4] "Monday, 2010-04-05" "Monday, 2010-05-03" "Monday, 2010-06-07"
[7] "Monday, 2010-07-05" "Monday, 2010-08-02" "Monday, 2010-09-06"
[10] "Monday, 2010-10-04" "Monday, 2010-11-01" "Monday, 2010-12-06"
[13] "Monday, 2011-01-03" "Monday, 2011-02-07" "Monday, 2011-03-07"
[16] "Monday, 2011-04-04" "Monday, 2011-05-02" "Monday, 2011-06-06"
[19] "Monday, 2011-07-04" "Monday, 2011-08-01" "Monday, 2011-09-05"
[22] "Monday, 2011-10-03" "Monday, 2011-11-07" "Monday, 2011-12-05"
[25] "Monday, 2012-01-02" "Monday, 2012-02-06" "Monday, 2012-03-05"
[28] "Monday, 2012-04-02" "Monday, 2012-05-07" "Monday, 2012-06-04"
[31] "Monday, 2012-07-02" "Monday, 2012-08-06" "Monday, 2012-09-03"
[34] "Monday, 2012-10-01" "Monday, 2012-11-05" "Monday, 2012-12-03"
[37] "Monday, 2013-01-07" "Monday, 2013-02-04" "Monday, 2013-03-04"
[40] "Monday, 2013-04-01" "Monday, 2013-05-06" "Monday, 2013-06-03"
[43] "Monday, 2013-07-01" "Monday, 2013-08-05" "Monday, 2013-09-02"
[46] "Monday, 2013-10-07" "Monday, 2013-11-04" "Monday, 2013-12-02"
[49] "Monday, 2014-01-06" "Monday, 2014-02-03" "Monday, 2014-03-03"
[52] "Monday, 2014-04-07" "Monday, 2014-05-05" "Monday, 2014-06-02"
[55] "Monday, 2014-07-07" "Monday, 2014-08-04" "Monday, 2014-09-01"
[58] "Monday, 2014-10-06" "Monday, 2014-11-03" "Monday, 2014-12-01"
[61] "Monday, 2015-01-05" "Monday, 2015-02-02" "Monday, 2015-03-02"
[64] "Monday, 2015-04-06" "Monday, 2015-05-04" "Monday, 2015-06-01"
[67] "Monday, 2015-07-06" "Monday, 2015-08-03" "Monday, 2015-09-07"
[70] "Monday, 2015-10-05" "Monday, 2015-11-02" "Monday, 2015-12-07"
© 2022 - 2024 — McMap. All rights reserved.