[R] date and time coding question

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Thu May 16 18:17:59 CEST 2013


I stand by my previous statement: you should avoid thinking of difftime objects as having units. At the point when you learn to regard the units argument as being associated with the numeric values you pass TO as.difftime or that you extract FROM a difftime object with as.numeric, then you will understand POSIXct arithmetic.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

"MacQueen, Don" <macqueen1 at llnl.gov> wrote:

>The difftime() function has a units argument, so it seems kind of
>obvious
>to me to think of objects of class 'difftime' as having units. And
>indeed
>they do, stored as an attribute:
>
>> str(difftime( Sys.time()+3700, Sys.time(), units='min'))
>Class 'difftime'  atomic [1:1] 61.7
>  ..- attr(*, "units")= chr "mins"
>
>> str(difftime( Sys.time()+3700, Sys.time(), units='sec'))
>Class 'difftime'  atomic [1:1] 3700
>  ..- attr(*, "units")= chr "secs"
>
>The numeric values are already in the specified units, prior to any
>printing or other further use of the result.
>
>
>?difftime says,
>
>-quote-
>If 'units = "auto"', a suitable set of units is chosen, the
>largest possible (excluding '"weeks"') in which all the absolute
>differences are greater than one.
>
>
>-end quote-
>
>So it's pretty clear that the units are chosen when the difference is
>calculated, not later. Inspection of the difftime() function source
>code
>confirms this. One can also inspect print.difftime() and see that the
>numeric value is used as is, without any unit conversion.
>
>
>Which returns me to my original point: when using diff() on a POSIXt
>object, I don't know how to specify the units, whereas with difftime()
>I
>do. If there is a way to specify units using diff(), I'd love to know
>about it!
>
>
>-Don



More information about the R-help mailing list