0

I am trying to get 3 month back, and tried many different solutions posted here. The one that generally works fine is dt %m+% months(-3) from this post. however, it is not working well again for this month. I wonder if there is a ultimate final working fine solution.

dt_1   <- as.Date("2018-06-30")
dt_2   <- as.character(dt_1 %m+% months(-3))

dt_2 became "2018-03-30" instead of "2018-03-31".

C_Mu
  • 305
  • 4
  • 14

1 Answers1

1

You can use floor_date to get to the first day of your input month, then subtract 2 months and 1 day.

library(lubridate)

dt_1  <- as.Date("2018-06-30")
as.character(floor_date(dt_1, 'month') - months(2) - 1)

#[1] "2018-03-31"
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38