Description Usage Arguments Details Value Author(s) References See Also Examples

Clueless adding numbers of months to a date will in some cases lead to invalid dates, think of e.g. 2012-01-30 + 1 month.

AddMonths ensures that the result is always a valid date, e.g.
`as.Date("2013-01-31") + 1 month`

will be `"2013-02-28"`

. If number `n`

is negative, the months will be subtracted.

1 |

`x` |
a Date object (or something which can be coerced by |

`n` |
the number of months to be added. If n is negative the months will be subtracted. |

`...` |
the dots are passed to |

All parameters will be recyled if necessary.

a vector of class `Date`

with the same dimension as `x`

, containing the transformed dates.

Andri Signorell <[email protected]>, based on code by Roland Rapold and Antonio

Thanks to Antonio: http://stackoverflow.com/questions/14169620/add-a-month-to-a-date

`AddMonthsYM`

; Date functions: `Year`

, `Month`

, etc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ```
# characters will be coerced to Date
AddMonths("2013-01-31", 1)
# negative n
AddMonths(as.Date("2013-03-31"), -1)
# Arguments will be recycled
# (with warning if the longer is not a multiple of length of shorter)
AddMonths(c("2013-01-31", "2013-03-31", "2013-10-31", "2013-12-31"), c(1,-1))
x <- as.POSIXct(c("2015-01-31", "2015-08-31"))
n <- c(1, 3)
AddMonths(x, n)
# mind the origin if x supplied as numeric ...
x <- as.numeric(as.Date(x))
AddMonths(x, n, origin=as.Date("1970-01-01"))
``` |

DescTools documentation built on March 19, 2018, 9:03 a.m.

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.