[R] Trouble with difftime()
Adam D. I. Kramer
adik at ilovebacon.org
Fri Sep 19 02:57:03 CEST 2008
I apologize, my original google searches were insufficient to turn up this
archived message:
http://tolstoy.newcastle.edu.au/R/help/06/04/24642.html
...in which my question is answered. Yes, this is the expected behavior,
because range() is designed to operate on difftime objects with any
unit...and it does so by converting to the theoretically-smallest unit.
My (and that poster's) confusion is likely due to the fact that, for cases
where seconds is not the highest common factor, the conversion is
surprising.
I think part of my surprise is also due to the fact that using the lowest
common multiple doesn't necessarily seem appropriate; why not, for instance,
convert everything to days, to weeks, or to the smallest or largest unit
passed to the summary function?
Also, please note here that I am not intending to whine or complain about
the functionality here, I'm just trying to understand it--much better to
convert all vectors to the same unit (ANY unit!). Having a better idea of
why choices like this have been made in R's user interface help me to better
"guess" what's up, to be able to solve my problems faster, and on my own.
--Adam
On Thu, 18 Sep 2008, Adam D. I. Kramer wrote:
> Hello again,
>
> I'm interested in manipulating some date objects, and have been
> playing with the difftime function. I'm not sure that this is the desired
> behavior:
>
>> pa$days.before.apr30 <-
>> difftime(may01,as.POSIXct(pa$training.max,origin=as.POSIXct("1969-12-31
>> 16:00:00",tz="PDT")),units="days")
>> units(pa$days.before.apr30)
> [1] "days"
>> units(pa$days.before.apr30)
> [1] "days"
>> range(pa$days.before.apr30,na.rm=TRUE)
> Time differences in secs
> [1] 1817 15723146
>
> ...the oddity being that the range is displayed in seconds, while the "unit"
> attached is days. The ?difftime info says that, "If the units are changed,
> the numerical value is scaled accordingly." This appears to be true, as the
> numbers reported by range() do indeed correspond to the desired output *
> 60*60*24 (days converted to seconds).
>
> Also, plotting a histogram via
>
> hist(pa$days.before.apr30)
>
> ...produces a histogram of seconds, not days.
>
> Using as.numeric(pa$days.before.apr30) or
> as.numeric(pa$days.before.apr30,units="days") to convert the difftime object
> into a number both as expected, so I just used that in order to be working
> (reliably) with days, but this seems like it might be a bug, so I thought I
> would bring it to your attention.
>
> Also, I spent a bit of time freaking out about the histogram being on a
> scale of 1e7, so it might be wise to print a warning if a unit conversion
> occurs implicitly even if this behavior is as intended.
>
> --Adam
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list