What do you call the previous month in the R?

Asked

Viewed 114 times

4

I need the R to tell me month before we are, that is, we are in "2018-05" and I need it to give me "2018-04".
So far my solution has been:

format(Sys.Date()-as.integer(format(Sys.Date(), "%d")),"%Y-%m")

I believe there should be a more "clean".

2 answers

3

With lubridate, can do so:

library(lubridate)
month(today() - months(1))

3


You can also use the mondate:

library(mondate)

mondate(Sys.Date())-1
# mondate: timeunits="months"
# [1] 05/12/2018

And to format only year and month:

format(mondate(Sys.Date())-1, "%Y-%m")
# [1] "2018-05"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.