[R] Truncating dates (and other date-time manipulations)

hadley wickham h.wickham at gmail.com
Thu Sep 11 23:59:22 CEST 2008


On Thu, Sep 11, 2008 at 2:26 PM, Jeffrey J. Hallman <jhallman at frb.gov> wrote:
> Look at the ti (Time Index) class in package tis.  Here's some examples I just did:
>> x <- Sys.Date()
>> x
> [1] "2008-09-11"
>> ti(x, "wsaturday")       ## a ti for the week that x falls into
> [1] 20080913
> class: ti
>> ti(x + 1, "wsaturday") - 1  ## ti for the latest complete week
> [1] 20080906
> class: ti
>> as.Date(ti(x + 1, "wsaturday") - 1)  ## as a Date
> [1] "2008-09-06"
>> ymd(ti(x + 1, "wsaturday") - 1)  ## or just the yyyymmdd number
> [1] 20080906

Hmmm:

dates <- structure(c(8516, 8544, 8568, 8596, 8609, 8666, 8701, 8750, 8754,
8798, 8811, 8817, 8860, 8873, 8918, 8931, 8966, 9020, 9034, 9056
), class = "Date")

> range(dates)
[1] "1993-04-26" "1994-10-18"
> as.Date(ti(range(dates), "monthly"))
[1] "1993-04-30" "1994-10-31"
> as.Date(ti(range(dates), "annual"))
[1] "1993-12-31" "1994-12-31"

What's the easiest way to control whether the dates are rounded up or
down?  And to get the starting period I just subtract one from the ti
object and then add one to the Date object?

Is there any standard way to do multiples of periods?  (e.g. 3 weeks,
4 days).  And is the finest resolution of a ti object a day?  I don't
think I see any periods less than that in the output of tif()

Hadley



-- 
http://had.co.nz/



More information about the R-help mailing list