R - lubridate - 将 Period 转换为数字计算月份

R - lubridate - Convert Period into numeric counting months

考虑以下:

library(lubridate)
period1 = weeks(2)
as.numeric(period1, "weeks")
> 2   # as expected 

现在我正在尝试对月份做类似的事情:

period2= months(6)
as.numeric(period2, "months")
as.numeric(period2, "weeks")
>  26.08929  # OK
as.numeric(period2,"months")
>  0.04166667  # Not OK?

这是 lubridate 中的错误吗?或者有什么不对我是doing/missing?

注意:我已经看到(旧)评论 Have lubridate subtraction return only a numeric value,所以我想我也可以使用变通方法来使用 difftime,但我想坚持使用 lubridate。

而不是使用 as.numeric() 尝试使用 lubridate 包中的 time_length()

period2= months(6)
time_length(period2,unit="days")
#182.6
time_length(period2,unit="weeks")
#26.09
time_length(period2,unit="months")
#6.004
class(time_length(period2,unit="months"))
#"numeric"