【问题标题】:R - lubridate - Convert Period into numeric counting monthsR - lubridate - 将期间转换为数字计数月份
【发布时间】:2023-03-12 20:19:01
【问题描述】:

考虑以下:

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 中的错误吗?还是我做错了什么/错过了什么?

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

【问题讨论】:

标签: r lubridate


【解决方案1】:

不要使用 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"

【讨论】:

  • 感谢伊山!我什至没有注意到lubridate 中的这个函数——知道的太多了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-09
  • 2022-08-09
  • 2014-03-30
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多