Number of Days Between Dates: Incorrect Results For Date Calucations.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Number of Days Between Dates: Incorrect Results For Date Calucations.

gerald.herbert
In some cases, incorrect results are produced by the code below intended to
calculate the number of days between 2 dates.  The year in question was a
leap year.

Note the results for 2004-04-04 and 2004-04-05 are the same! They should be
37 and 38 respectively.

> as.integer(as.POSIXct("2004-04-02") - as.POSIXct("2004-02-27"))
[1] 35
> as.integer(as.POSIXct("2004-04-03") - as.POSIXct("2004-02-27"))
[1] 36
> as.integer(as.POSIXct("2004-04-04") - as.POSIXct("2004-02-27"))
[1] 37
> as.integer(as.POSIXct("2004-04-05") - as.POSIXct("2004-02-27"))
[1] 37
> as.integer(as.POSIXct("2004-04-06") - as.POSIXct("2004-02-27"))
[1] 38



> as.integer(difftime(as.POSIXct("2004-04-06"),
as.POSIXct("2004-02-27"),units="days"))
[1] 38
> as.integer(difftime(as.POSIXct("2004-04-04"),
as.POSIXct("2004-02-27"),units="days"))
[1] 37
> as.integer(difftime(as.POSIXct("2004-04-05"),
as.POSIXct("2004-02-27"),units="days"))
[1] 37

It appears that difftime() and "-" are producing invalid results.


Regards,

Gerald Herbert

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Number of Days Between Dates: Incorrect Results For Date Calucations.

Gabor Grothendieck
The results are actually correct if you consider daylight savings time.

For example, try this and note that the difference is 23 hours, not 24 hours:

   as.POSIXct("2004-04-05") - as.POSIXct("2004-04-04")

You can address this by either using Date or chron classes or adding
the tz = "GMT" argument on your as.POSIXct calls as GMT does not
have daylight savings time.

See the Help Desk article in R News 4/1 for more on this.


On 2/21/06, [hidden email]
<[hidden email]> wrote:

> In some cases, incorrect results are produced by the code below intended to
> calculate the number of days between 2 dates.  The year in question was a
> leap year.
>
> Note the results for 2004-04-04 and 2004-04-05 are the same! They should be
> 37 and 38 respectively.
>
> > as.integer(as.POSIXct("2004-04-02") - as.POSIXct("2004-02-27"))
> [1] 35
> > as.integer(as.POSIXct("2004-04-03") - as.POSIXct("2004-02-27"))
> [1] 36
> > as.integer(as.POSIXct("2004-04-04") - as.POSIXct("2004-02-27"))
> [1] 37
> > as.integer(as.POSIXct("2004-04-05") - as.POSIXct("2004-02-27"))
> [1] 37
> > as.integer(as.POSIXct("2004-04-06") - as.POSIXct("2004-02-27"))
> [1] 38
>
>
>
> > as.integer(difftime(as.POSIXct("2004-04-06"),
> as.POSIXct("2004-02-27"),units="days"))
> [1] 38
> > as.integer(difftime(as.POSIXct("2004-04-04"),
> as.POSIXct("2004-02-27"),units="days"))
> [1] 37
> > as.integer(difftime(as.POSIXct("2004-04-05"),
> as.POSIXct("2004-02-27"),units="days"))
> [1] 37
>
> It appears that difftime() and "-" are producing invalid results.
>
>
> Regards,
>
> Gerald Herbert
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Number of Days Between Dates: Incorrect Results For DateCalucations.

Dimitris Rizopoulos
In reply to this post by gerald.herbert
check again your results with as.integer() replaced by round(); check
also ?as.integer for its usage.

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message -----
From: <[hidden email]>
To: <[hidden email]>
Sent: Tuesday, February 21, 2006 4:48 PM
Subject: [R] Number of Days Between Dates: Incorrect Results For
DateCalucations.


> In some cases, incorrect results are produced by the code below
> intended to
> calculate the number of days between 2 dates.  The year in question
> was a
> leap year.
>
> Note the results for 2004-04-04 and 2004-04-05 are the same! They
> should be
> 37 and 38 respectively.
>
>> as.integer(as.POSIXct("2004-04-02") - as.POSIXct("2004-02-27"))
> [1] 35
>> as.integer(as.POSIXct("2004-04-03") - as.POSIXct("2004-02-27"))
> [1] 36
>> as.integer(as.POSIXct("2004-04-04") - as.POSIXct("2004-02-27"))
> [1] 37
>> as.integer(as.POSIXct("2004-04-05") - as.POSIXct("2004-02-27"))
> [1] 37
>> as.integer(as.POSIXct("2004-04-06") - as.POSIXct("2004-02-27"))
> [1] 38
>
>
>
>> as.integer(difftime(as.POSIXct("2004-04-06"),
> as.POSIXct("2004-02-27"),units="days"))
> [1] 38
>> as.integer(difftime(as.POSIXct("2004-04-04"),
> as.POSIXct("2004-02-27"),units="days"))
> [1] 37
>> as.integer(difftime(as.POSIXct("2004-04-05"),
> as.POSIXct("2004-02-27"),units="days"))
> [1] 37
>
> It appears that difftime() and "-" are producing invalid results.
>
>
> Regards,
>
> Gerald Herbert
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Number of Days Between Dates: Incorrect Results For Date Calucations.

Peter Dalgaard
In reply to this post by gerald.herbert
<[hidden email]> writes:

> In some cases, incorrect results are produced by the code below intended to
> calculate the number of days between 2 dates.  The year in question was a
> leap year.
 
> Note the results for 2004-04-04 and 2004-04-05 are the same! They should be
> 37 and 38 respectively.

Nope. First, it depends on your timezone. Over here, they do actually
differ. However, a few weeks earlier, we have the similar phenomenon

> as.POSIXct("2004-03-29") - as.POSIXct("2004-02-27")
Time difference of 30.95833 days
> as.POSIXct("2004-03-28") - as.POSIXct("2004-02-27")
Time difference of 30 days

which is of course because March 28 was only 23 hours long which is in
turn because

> as.POSIXct("2004-03-28")
[1] "2004-03-28 CET"
> as.POSIXct("2004-03-29")
[1] "2004-03-29 CEST"
 
Get it?



> > as.integer(as.POSIXct("2004-04-02") - as.POSIXct("2004-02-27"))
> [1] 35
> > as.integer(as.POSIXct("2004-04-03") - as.POSIXct("2004-02-27"))
> [1] 36
> > as.integer(as.POSIXct("2004-04-04") - as.POSIXct("2004-02-27"))
> [1] 37
> > as.integer(as.POSIXct("2004-04-05") - as.POSIXct("2004-02-27"))
> [1] 37
> > as.integer(as.POSIXct("2004-04-06") - as.POSIXct("2004-02-27"))
> [1] 38
>
>
>
> > as.integer(difftime(as.POSIXct("2004-04-06"),
> as.POSIXct("2004-02-27"),units="days"))
> [1] 38
> > as.integer(difftime(as.POSIXct("2004-04-04"),
> as.POSIXct("2004-02-27"),units="days"))
> [1] 37
> > as.integer(difftime(as.POSIXct("2004-04-05"),
> as.POSIXct("2004-02-27"),units="days"))
> [1] 37
>
> It appears that difftime() and "-" are producing invalid results.
>
>
> Regards,
>
> Gerald Herbert
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html