[R] Mystified - comparing chron times

Sarah Goslee sarah.goslee at gmail.com
Fri Sep 16 18:30:15 CEST 2011

I'll freely admit that I know little about chron and didn't have a
computer with R in
front of me at the time.

But the original querent converted the chron times to numeric and then used ==,
and thus it became a 7.31 issue. Converting to character is a different way to
approach the problem; which is "better" depends on more context than we
have available, as David points out.


On Fri, Sep 16, 2011 at 10:58 AM, David Winsemius
<dwinsemius at comcast.net> wrote:
> On Sep 16, 2011, at 10:08 AM, B77S wrote:
>> I'm sure Sarah's solution works (and she knows more about R than myself),
>> but
>> I ran into a similar problem and used:
>> as.character(start.time)==as.character(expected_start.time)
> What you did was an implicit rounding of the values to the digits level
> specified by some process, possibly a default formatting spec, guessing to
> round(num, 7) . I tried to see whether package chron might specify i,t but
> this effort only produced this
>> cur.opts <- names(options())
>> require(chron)
> Loading required package: chron
>>  names(options())[!names(options()) %in% cur.opts]
> [1] "chron.year.abb"    "chron.year.expand"
> What Sarah suggested was essentially rounding to 16 digits to the right of
> the decimal.
> If the OP had offered dput() on these objects, there would  have been a lot
> less guessing.
> --
> David
>> good luck regardless.
>> -BS
>> Sarah Goslee wrote:
>>> Sounds like a case for FAQ 7.31, or, yet another machine precision issue.
>>> Try all.equal() instead of ==
>>> Sarah
>>> On Fri, Sep 16, 2011 at 7:36 AM, mebstyne <mebstyne at me.com> wrote:
>>>> I have two local variables: startTime and expectedStartTime.  Both are
>>>> chron
>>>> related objects.
>>>> When I look at the class for the objects I can see they are of class
>>>> "times".
>>>> When I print them to the console, they both read: "09:30:00"
>>>> When I print them as.numeric(), they both read: 0.3958333
>>>> When I try and compare them: (as.numeric(startTime) ==
>>>> as.numeric(expectedStartTime)) it returns FALSE.
>>>> I'm mystified.  I would expect them to be true.
>>>> Perhaps a key to the riddle is how the two objects were created.
>>>> "startTime" was created by reading a text field from a socket,
>>>> converting
>>>> it
>>>> into a chron object using chron(x, "%m/$d/%Y %H:%M:%S"), then finally I
>>>> created a time out of the chron by doing a quick butchering of the
>>>> "integer"
>>>> portion of the numeric: (times(as.numeric(x) - as.integer(x))
>>>> "expectedStartTime" was created by the command times('09:30:00')
>>>> Any suggestions?  Tips?  Alternative approaches?  I've pulled too many
>>>> hairs
>>>> triaging this.
>>>> All hands welcomed on this little challenge.
>>>> Big picture goal of what I'm doing: I have a list of chron objects with
>>>> both
>>>> dates and times portion filled out and I'm trying to determine if the
>>>> time
>>>> is a specific time (specific to the minute).
>>>> Thanks!
>>>> -Michael

Sarah Goslee

More information about the R-help mailing list