Я закончил тем, что создал быструю функцию, которая, кажется, работает:
Find.Date.x.month.ago <- function(current_date, x){
Current.month <- month(current_date)
Past.x.month <- Current.month - x
if(Past.x.month < 0){
Past.x.month <- 12 + Past.x.month
}
trgt.day <- day(current_date)
while (is.na(lubridate::ymd(paste(year(current_date), Past.x.month, trgt.day, sep="-"))) == TRUE) {
trgt.day <- trgt.day - 1
}
return(lubridate::ymd(paste(year(current_date), Past.x.month, trgt.day, sep="-")))
}
Попытка с 30 мая подтвердить, обрабатывает ли она проблему февраля:
test.date <- "2019-05-30"
> suppressWarnings(Find.Date.x.month.ago(test.date, 3))
[1] "2019-02-28"
> suppressWarnings(Find.Date.x.month.ago(test.date, 6))
[1] "2019-11-30"