[R] Reformatting dates using chron

Prof Brian D Ripley ripley at stats.ox.ac.uk
Thu Jan 11 19:33:35 CET 2001

On Fri, 12 Jan 2001, Andrew Criswell wrote:

> Hello All:
> I am trying to generate a sequence of dates using library(chron). Using the following code, I got most of what I want,
>          dts <- seq.dates("01/02/1998", "01/10/2001", by="day")
>          dts <- dts[!is.weekend(dts)]
> a sequence of weekdays only (no weekend dates), with first observation appearing as 01/02/98 and the last 01/10/01. But I would like it to appear as 1998/01/02 to 2001/01/10.
> Is there some way to change the order of the output and allow "year" to appear with four digits and slash signs between year, month and day?
> (I am using R 1.2.0 on a Windows 2000 platform.)

That's how they print.

attr(dts, "format") <- "y/m/d"


> dts[1]
[1] 98/01/02

I can't get `year' to work in there, so try

format(as.POSIXct(dts), "%Y/%m/%d")

which does work and is very flexible.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list