Search code examples
rdplyrlubridate

How to return the previous month using R?


So in R I have a dataframe as follows:

Month1
December
October
April
February
January

All I want is to return the previous month.

Is there a function for this? Perhaps in lubridate? This is what I want in the end:

Month1     Month2
December   November
October    September
April      March
February   January
January    December

Solution

  • Another way would be:

    c('December', 'November', 'September') -> x   
    
    library(lubridate)
    months(dym(paste0('01-2020-', x)) - 1)
    #"November" "October"  "August"