lubridate::floor_date returns 那

lubridate::floor_date returns NA

我正在尝试编写一个函数,给定一个日期 start 和一个整数 n,将日期加上 n 个月,然后取结果的最后一天月。 然而,下面的一段代码 returns NA for n=8+12k,但似乎在其他情况下也有效。

library(lubridate)
start <- ymd("2020-06-29")
n <- 8
floor_date(start + months(n), "month") + months(1) - days(1)
>[1] NA

我猜想这多少是因为闰年的存在,但我还是觉得很费解。另外,我在文档中找不到任何关于此的内容。

有人可以解释一下发生了什么,或者建议更好的方法来完成这项工作吗?

时钟包对此非常有用,因为它有一个明确的参数来指定您希望对无效日期执行的操作。还有 date_end 函数。
我想你想要:(R >= 4.1 for native pipe)

library(clock)
start <- parse_date("2020-06-29")
n <- 8
start |> add_months(n, invalid = "previous") |> date_end('month')

给出: [1] "2021-02-28"