Extract first Monday of every month

后端 未结 3 1347
无人及你
无人及你 2021-01-13 14:17

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

相关标签:
3条回答
  • 2021-01-13 14:18

    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"
    
    0 讨论(0)
  • 2021-01-13 14:25

    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"...
    
    0 讨论(0)
  • 2021-01-13 14:28

    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"
    
    0 讨论(0)
提交回复
热议问题