Skip to content
Prev 156483 / 398506 Next

Adding 1 month to a dataframe column

Try this:

  # example data - Jan 31/00, Feb 29/00, Mar 31/00, ..., Dec 31/00
  d <- seq(as.Date("2000-02-01"), by = "month", length = 12) - 1
  d

  # find beginning of next month
  nextmon  <- function(d) as.Date(cut(as.Date(cut(d, "month")) + 32, "month"))

  # add number of days from start of current month to next month
  nextmon(d) + as.POSIXlt(d)$mday - 1

  # If all your dates are at month end and  you want the plus 1 month
  # to be at month end too then replace last line with:

  nextmon(nextmon(d)) - 1

The last one can be done even more easily with as.yearmon from zoo.
It converts the dates to a year plus 0 for jan, 1/12 for feb, etc. Also,
as.Date.yearmon(..., frac = 1) converts back from yearmon class to
Date class using end of month for the result so:

  library(zoo)
  as.Date(as.yearmon(d) + 1/12, frac = 1)
On Thu, Sep 18, 2008 at 12:24 PM, <ANGELO.LINARDI at bancaditalia.it> wrote: