简体   繁体   English

R“difftime”表现出奇怪的行为

[英]R “difftime” exhibits strange behavior

I am a bit confused by the function "difftime". 我对“difftime”功能感到有点困惑。 When I calculate the time difference up to 27 March 2016 everything is ok. 当我计算截至2016年3月27日的时差时,一切都还可以。 But as soon as I try 28, 29, 30 or 31 March 2016 there seems to be a problem: 但是,一旦我尝试2016年3月28日,29日,30日或31日,似乎就出现了问题:

> difftime("2016-03-27","1979-01-01", units="days")
Time difference of 13600 days
> difftime("2016-03-28","1979-01-01", units="days")
Time difference of 13600.96 days
> difftime("2016-03-31","1979-01-01", units="days")
Time difference of 13603.96 days

I can get around this problem by setting the date to 27 March 2016 and then adding manually the number of days "missing", but I was wondering if there is maybe something wrong with the function...? 我可以通过将日期设置为2016年3月27日然后手动添加“丢失”的天数来解决这个问题,但我想知道函数是否有问题...? I don't really see what I could have done wrong since I just changed the day number... 因为我刚刚更改了天数,所以我真的没有看到我能做错的事情......

The reason an extra day in going from 2016-03-27 to 2016-03-28 is 0.96 is due to daylight savings time kicking in: 2016-03-272016-03-28的额外一天是0.96的原因是由于夏令时开始:

0.96 = 23 hours / 24 hours

Read this R Nabble blog which discusses this problem in detail. 阅读此R Nabble博客 ,详细讨论了这个问题。

There is no problem, when you convert it with as.Date : 使用as.Date转换它时没有问题:

difftime(as.Date("2016-03-28"), as.Date("1979-01-01"), units="days")

# Time difference of 13601 days

Also there is no problem with lubridate : lubridate也没有问题:

library(lubridate)   
difftime(ymd("2016-03-28"), ymd("1979-01-01"), units="days")

# Time difference of 13601 days

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

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