hadley wickham
2008-Sep-11 15:02 UTC
[R] Truncating dates (and other date-time manipulations)
Dear all, 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? Otherwise, I have been working on a bottom up set of functions to make date manipulation easier - http://gist.github.com/10238. These should work for all date-time types and I've tried to use a consistent naming scheme and consistent output conventions. This seems like a rather small bundle of functions to be worth turning into a package, so I was wondering if anyone would be interested in adopting them into an existing date-time related package. Regards, Hadley -- http://had.co.nz/
Whit Armstrong
2008-Sep-11 15:28 UTC
[R] Truncating dates (and other date-time manipulations)
I'm wrapping boost date_time into an R package. I'll post it up to cran shortly. http://www.boost.org/doc/libs/1_36_0/doc/html/date_time.html I'm not sure if that is what you are looking for, but there are a lot of useful utilities in this library. -Whit On Thu, Sep 11, 2008 at 11:02 AM, hadley wickham <h.wickham at gmail.com> wrote:> Dear all, > > 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? > > Otherwise, I have been working on a bottom up set of functions to make > date manipulation easier - http://gist.github.com/10238. These should > work for all date-time types and I've tried to use a consistent naming > scheme and consistent output conventions. This seems like a rather > small bundle of functions to be worth turning into a package, so I was > wondering if anyone would be interested in adopting them into an > existing date-time related package. > > Regards, > > Hadley > > -- > http://had.co.nz/ > > ______________________________________________ > 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. >
Gabor Grothendieck
2008-Sep-11 16:00 UTC
[R] Truncating dates (and other date-time manipulations)
See ?cut.Date In the zoo package see: ?as.yearmon ?as.yearqtr ?aggregate.zoo and the many examples in: ?plot.zoo ?xyplot.zoo as well as the three zoo vignettes. Also in the xts package look at ?to.period For regularly spaced series the tis package supports a wide variety of time bases and can convert among them. There are as.zoo.tis and as.tis.zoo routines in zoo and tis; also, xts is a subclass of zoo so all these packages can work together. On Thu, Sep 11, 2008 at 11:02 AM, hadley wickham <h.wickham at gmail.com> wrote:> Dear all, > > 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? > > Otherwise, I have been working on a bottom up set of functions to make > date manipulation easier - http://gist.github.com/10238. These should > work for all date-time types and I've tried to use a consistent naming > scheme and consistent output conventions. This seems like a rather > small bundle of functions to be worth turning into a package, so I was > wondering if anyone would be interested in adopting them into an > existing date-time related package. > > Regards, > > Hadley > > -- > http://had.co.nz/ > > ______________________________________________ > 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. >
Jeffrey J. Hallman
2008-Sep-11 19:26 UTC
[R] Truncating dates (and other date-time manipulations)
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