我在使用lubridate包时遇到了意外的结果。下面是一个简短的示例,使用函数time_length计算物理经过的时间;首先直接计算,然后按时间间隔计算。似乎使用“秒”单位的方法是相同的,但使用“月”单位的方法是不同的。有人知道为什么会这样吗?
作为参考,我使用:
R version: 3.5.1
lubridate version: 1.7.4可重现的例子:
library(lubridate)
## generate two dates to illustrate issue
startDate <- ymd("2015-02-27")
endDate <- ymd("2015-03-02")
## calculate physical passage of time, in seconds
time_length(endDate - startDate, "seconds") # result: 259200
time_length(interval(startDate, endDate), "seconds") # result: 259200
# The methods above match, but...
## calculate physical passage of time, in months
time_length(endDate - startDate, "months") # result: 0.09863014
time_length(interval(startDate, endDate), "months") # result: 0.1071429
# The results no longer match! Why?发布于 2019-03-12 08:33:51
月份没有固定的长度。您正在尝试将259200秒(即3天)转换为月
> endDate - startDate
Time difference of 3 days您的0.09863014为3/(365/12),试图将任意3天转换为任意月份
> interval(startDate, endDate)
[1] 2015-02-27 UTC--2015-03-02 UTC您的0.1071429是3/28,因为您试图将从非闰年2月开始的3天转换为月份
https://stackoverflow.com/questions/55112325
复制相似问题