Home > Mobile >  numeric calendar vector for dynamic objects
numeric calendar vector for dynamic objects

Time:11-01

I've got some dynamic objects like this:

today <- as.Date(Sys.time())
this_month <- as.numeric(format(today,"%m"))
next_month <- this_month 1
month_after <- next_month 1 

As you can probably guess, this is causing an issue given the fact there's no 13th month, therefore I need to provide a vector of values for the "next_motn" and "month_after" objects to choose from. I'm thinking of something like this:

months <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

But I'm unsure of the syntax of how to make the code function.

CodePudding user response:

You can use lubridate::months to add months to your original date, and then get the month:

library(lubridate)
as.numeric(format(today   months(2), "%m"))
#[1] 1
  • Related