[Rd] diff(time) vs. difftime?

Spencer Graves spencer.graves at structuremonitoring.com
Tue Mar 20 00:07:59 CET 2012


	  I've just uncovered an infelicity in as.difftime:


 > as.difftime(diff(as.POSIXct(c('2012-12-12', '2012-12-13'))), 
units='hours')
Time difference of 1 days


	  Is this a bug in the code or in my understanding?


	  Thanks again for all your hard work in making R the great product it 
is and in answering so many questions.


	  Best Wishes,
	  Spencer


 > sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] zoo_1.7-7      RODBC_1.3-4    WriteXLS_2.1.0 sos_1.3-1 
brew_1.0-6

loaded via a namespace (and not attached):
[1] grid_2.14.1    lattice_0.20-0 tools_2.14.1


############################


       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
08:00:01'))))
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.


  methods('-')
[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.


       Thanks,
       Spencer


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