Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

numeric calendar vector for dynamic objects

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading