[R] How to manipulate the time data without the date?

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 7 12:12:48 CET 2008


On Fri, Nov 7, 2008 at 3:32 AM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> 2008/11/7 tedzzx <zengzhenxing at gmail.com>:
>>
>> The problem is that: There is some rounding problems, for example
>> Library(chron)
>> any(times("4:00:01")==times("4:00:00")+times("00:00:01")))
>> False
>>
>> But,it should be true
>
>  FAQ 7.31 in disguise!
>
>  chron stores date-times as fractions, so you're comparing two
> floating point numbers here. FAQ 7.31 applies.
>
>  Use 'all.equal', which has a method for chron that uses a 1 second
> tolerance to match times:
>

That solution works but just to be precise, note that a "times"
class object need not be a "dates" or "chron" class object and
while there is a chron:::all.equal.dates there is no chron:::all.equal.times.

The reason all.equal works here is that all.equal.default works here and
it works here by ultimately calling all.equal.numeric to do the real work.

> y <- times("4:00:00")+times("00:00:01")
> class(y) # no "dates" or "chron" in class vector
[1] "times"

Also all.equal.dates uses a tenth of a second as the tolerance:

> chron:::all.equal.dates
function (..., tolerance = 1/(10 * 24 * 60 * 60))
NextMethod("all.equal", ..., tolerance = tolerance)
<environment: namespace:chron>


>> all.equal(times("4:00:01"),times("4:00:00")+times("00:00:01"))
> [1] TRUE
>



More information about the R-help mailing list