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

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Wed 22 Feb 2006 - 03:11:49 EST

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


> 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
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Feb 22 03:46:37 2006

This archive was generated by hypermail 2.1.8 : Wed 22 Feb 2006 - 09:27:43 EST