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

Jeffrey J. Hallman jhallman at frb.gov
Thu Sep 11 21:26:37 CEST 2008


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

You can do the same for "wsunday", if you want weeks that end on Sunday.  In
all, ti supports 7 different weekly frequencies, for weeks ending on Monday,
Tuesday, ..., Sunday.  It also has daily and business day frequencies, 14
biweekly frequencies, semimonthly, monthly, bimonthly, and three quarterly
frequencies (for quarters that end in October, November and December).
Similarly, there are 12 annual frequencies and 6 semiannual ones, and a
biannual frequency.  Also supported are hourly, minutely and secondly
frequencies.

Gabor and I have done some work towards making zoo and ti objects compatible
with each other.



"hadley wickham" <h.wickham at gmail.com> writes:
> I've been struggling to perform common operations on dates that I need
> to be able to correct draw date-time scales - in particular I need to
> be able to round/truncate/ceiling dates to arbitrary precision - e.g.
> to weeks, months or years (or multiples thereof).  I haven't been able
> to find anything to do this in base R (trunc.Date only truncates to
> sub-day units), or in the date related contributed packages I've
> looked in (e.g. chron).  Have I missed something?
-- 
Jeff



More information about the R-help mailing list