[R] weird "max" behavior for difftime class

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Apr 4 08:29:26 CEST 2006

On Mon, 3 Apr 2006, Jacob Wegelin wrote:

> If you apply the "max" function to a vector of class "difftime" with
> units="days", the returned value is in units of "seconds".
> Is this not a bug?  At any rate it can lead to confusing results if one
> buries a call to "max" deep in some data analysis code.

Why do you think that 1 days and 86400 secs are different quantities? (If 
your code treats them differently, that is a bug in your code not in R.)

The reason that you did not find a method for max() is that it is part of 
a group generic Summary, and the group generics have methods:

> methods(class="difftime")
[1] *.difftime       /.difftime       [.difftime       Math.difftime
[5] mean.difftime    Ops.difftime     print.difftime   round.difftime
[9] Summary.difftime

The reason Summary.difftime switches to "secs" is that it operates on more 
than one vector which can be expressed in different time units.

Isn't calling this 'wierd' rather discourteous to those who thought it all 
out carefully?

> Details:
>> y<-structure(1, class = "difftime", units = "days")
>> max(y)
> Time difference of 86400 secs
>> y
> Time difference of 1 days
>> min(y)
> Time difference of 86400 secs
>> as.numeric(min(y))
> [1] 86400
>> dput(min(y))
> structure(86400, units = "secs", class = "difftime")
>> methods("max")
> no methods were found
>> methods("difftime")
> no methods were found
> Warning message:
> function 'difftime' appears not to be generic in: methods("difftime")
> Thanks for any insights
> Jake Wegelin
> ______________________________________________
> R-help at 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

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list