[Rd] diff(time) vs. difftime?

Spencer Graves spencer.graves at structuremonitoring.com
Mon Mar 19 23:27:10 CET 2012

       I just encountered another RTFM problem:  With 
diff(as.POSIXct(...), ...) I was unable to control the units of the 
results.  Examples:

 > (d.d <- diff(as.POSIXct(c('2012-12-12', '2012-12-13'))))
Time difference of 1 days
 > (d.h <- diff(as.POSIXct(c('2012-12-12 08:00', '2012-12-12 09:00'))))
Time difference of 1 hours
 > (d.m <- diff(as.POSIXct(c('2012-12-12 08:00', '2012-12-12 08:01'))))
Time difference of 1 mins
 > (d.s <- diff(as.POSIXct(c('2012-12-12 08:00:00', '2012-12-12 
Time difference of 1 secs
 > as.numeric(d.d)
[1] 1
 > as.numeric(d.s)
[1] 1

       methods('diff') identified the following:

[1] diff.Date    diff.default diff.POSIXt  diff.ts      diff.zoo*

       In looking at the help pages for each of these functions, I found 
no mention of "difftime" or any other way to control the units.

[1] -.Date     -.POSIXt   -.yearmon* -.yearqtr*

        ?"-.Date" didn't seem helpful on this, either.  ?"-.POSIXt" 
contained a link to "difftime", but I didn't see that until after I read 
the code for "-.POSIXt" and found "difftime".

       I humbly beseech ye to consider adding a difftime example to all 
these help pages.


p.s.  In case there is any doubt, I very much appreciate all the work 
that the R Core team has invested in making R what it is today.

Spencer Graves, PE, PhD
President and Chief Technology Officer
Structure Inspection and Monitoring, Inc.
751 Emerson Ct.
San José, CA 95126
ph:  408-655-4567
web:  www.structuremonitoring.com

More information about the R-devel mailing list