简体   繁体   English

R difftime关闭1年

[英]R difftime off by 1 year

I'm trying to calculate the difference-in-years between 2 dates. 我正在尝试计算2个日期之间的年差。

time1 <- as.Date(x = "2017-02-14",
                 format = "%Y-%m-%d",
                 origin = "",
                 tz = "")

time2 <- as.Date(x = "1972-02-17",
                 format = "%Y-%m-%d",
                 origin = "",
                 tz = "")

as.integer(x = difftime(time1 = time1,
                        time2 = time2,
                        tz = "",
                        units = "days")) / 365

According to the above code, the difference-in-years is about 45.02466. 根据上述代码,年差约为45.02466。

A glance at the 2 dates confirms that the difference-in-years is close to, but less than, 45. 仔细查看这两个日期,可以确认年差接近但小于45。

The difference-in-years value should begin with 44. 年差值应以44开头。

Why is difftime inflating the number of years by 1? 为什么difftime将年份数增加1?

Probably this happens since you divide by 365 days, whereas at least 44/4 = 11 years had a duration of 366 days. 可能发生这种情况是因为您除以365天,而至少44/4 = 11年的持续时间为366天。 You could consider to divide by 365.25, which is a more correct average duration of a year. 您可以考虑除以365.25,这是更正确的一年平均持续时间。

Also, you might like to look up the lubridate package to see the interval of how many years have passed between two dates 另外,您可能想查看lubridate软件包,以查看两个日期之间间隔了多少年的时间间隔

library(lubridate)
> time1 <- as.Date(x = "2017-02-14",
+                  format = "%Y-%m-%d",
+                  origin = "",
+                  tz = "")
> 
> time2 <- as.Date(x = "1972-02-17",
+                  format = "%Y-%m-%d",
+                  origin = "",
+                  tz = "")
> 
> 
> interval(time2, time1) %/% years(1)
[1] 44

There are an average of 365.25 days in a year due to leap years -- could be slightly different if the number of years is not a multiple of 4 but this is a reasonable long term average to use. 由于leap年,一年中平均有365.25天-如果年数不是4的倍数,则可能会略有不同,但这是一个合理的长期平均值。 By using 365 instead this inflated the answer in the question. 通过使用365相反,这会扩大问题的答案。

time1 <- as.Date("2017-02-14")
time2 <- as.Date("1972-02-17")
as.numeric(time1 - time2) / 365.25
## [1] 44.99384

Standardized years 标准化年

If you want to be really exact about leap years there were 12 leap days between time1 and time2 如果您想确切了解leap年,则time1time2之间有12个leap日

tt <- seq(time2, time1, by = "day")
no_leap_days <- sum(format(tt, "%m-%d") == "02-29")
no_leap_days
## [1] 12

so another way to do this is to calculate the number of days between the two dates, subtract the number of leap days effectively standardizing every year to 365 days and then divide by 365: 因此,另一种方法是计算两个日期之间的天数,将每年有效标准化的leap日数减去365天,然后除以365:

as.numeric(time1 - time2 - no_leap_days) / 365
## [1] 44.99178

Note 注意

Note that "Date" class does not have a time zone, origin is only used in as.Date if the argument is numeric and the format used in the question is the default format so format is not needed. 请注意, "Date"类没有一个时区, origin只在使用as.Date如果参数为数字,该问题所使用的格式是默认的格式, format是没有必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM