On Thu, May 21, 2009 at 5:04 PM, Stavros Macrakis <macra...@alum.mit.edu> wrote: > On Wed, May 20, 2009 at 12:28 PM, Gabor Grothendieck > <ggrothendi...@gmail.com> wrote: >> >> There is a times class in the chron package. > > Perfect! Just what I was looking for. > > On Wed, May 20, 2009 at 12:19 PM, jim holtman <jholt...@gmail.com> wrote: >> >> If you want the hours from a POSIXct, here is one way of doing it... >> > y <- difftime(x, trunc(x, units='days'), units='hours') > > Ah, trunc.POSIXt -- I missed that one, thanks. > >> It depends on what type of computations you want to do with it. You can >> leave it as POSIXct and carry out a lot of them. Can you specify what you >> want? > > I am comparing irregular time series from different days, looking at the > differences in intraday patterns. So I want to put them on a common 0-24h > scale and then do various kinds of plots and analyses, keeping the > conventional display form (10:30 etc.) when specific times display or > print. It looks as though chron:::times combined with trunc.POSIXt pretty > much solves my problem, except that `times` ignores the time units:
It uses hours/minutes/seconds for values < 1 day and uses days and fractions of a day otherwise. For values and operations that it has not considered it falls back to the internal representation. Most of your examples start to make sense once you realize this. > >> as.POSIXct('2009-3-23 12:23')-trunc(as.POSIXct('2009-3-23 12:23'),"day") > Time difference of 12.38333 hours >> times(as.POSIXct('2009-3-23 12:23')-trunc(as.POSIXct('2009-3-23 >> 12:23'),"day")) > Time in days: <<< seems to treat difftimes as raw numbers!! > [1] 12.38333 > > Obviously I can work around this, but shouldn't `times` give an error when > it encounters an object of unknown class rather than unsafely using its > internal representation? Of course, better still if `times` converted > correctly.... > > In general, `times` has other inconsistent and peculiar behavior: > > times(2) => Time in days: 2 Allows specifying multi-day periods, OK > times(1.5) => Time in days: 1.5 Allows specifying fractional multi-day > periods, OK > times(0.5) => "12:00:00" Inconsistent format compared to times(1.5) > times("18:00:00") + times("18:00:00") => Time in days: 1.5, OK > times("36:00:00") => error Why does it allow times(1.5) and > times("18:00:00") + times("18:00:00") to specify 1.5 days, but not 36 hours? > times(-0.5) => -0.5 Why doesn't it print Time in days: -0.5? > times("18:00:00")/times("1:00:00") => Time in days: 18 Incorrect > dimensions; meaningless result -- should be dimensionless > times("18:00:00") * times("10:00:00") => 07:30:00 Incorrect dimensions; > meaningless result. > sin(times("18:00:00")) => 16:21:34 Meaningless result -- should be error > > It's nice that R has a class system, but if code ignores the class.... > >> There is an article on dates and times in R News 4/1. > > Thanks for the pointer. > > -s > > ______________________________________________ R-help@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.