R: adding 1 month to a date

后端 未结 2 1791
忘掉有多难
忘掉有多难 2020-12-16 18:09

I want to get the date sequence between a startDate and endDate by adding 1 month to the startDate. ie, if startDate is 2

2条回答
  •  -上瘾入骨i
    2020-12-16 18:32

    I have to work with dates in R, and one of the most useful packages that I found for date data is lubridate. For your problem, you can simply do the following:

    require(lubridate)
    # ymd function parses dates in year-month-day format
    startDate <- ymd('2013-01-31')
    # The %m+% adds months to dates without exceeding the last day
    myDates <- startDate %m+% months(c(0:6))
    

    lubridate also has many other functions for dates, and I highly recommend taking a look.

提交回复
热议问题