Given any date, I use this code to get the beginning date of previous month from that date:
library(lubridate)
end_date <- '2021-10-30'
floor_date(as.Date(end_date) - months(1), 'month')
[1] "2021-09-01"
But I've found it return NA
, as the format of date is %Y-%m-31
:
end_date <- '2021-10-31'
floor_date(as.Date(end_date) - months(1), 'month')
[1] NA
How could we explain this error?