[R] Reformatting dates using chron

Kurt Hornik Kurt.Hornik at ci.tuwien.ac.at
Fri Jan 12 08:50:02 CET 2001

>>>>> Prof Brian D Ripley writes:

> 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"

> gives

>> 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.

Using the standard formatting tools from chron, you can do e.g.

	format(dts) <- "year month day"

which will give

R> dts
[1] 1998 January 02 1998 January 03 1998 January 04 1998 January 05

etc, but you cannot get a full year immediately followed by a non space
separtor.  You could write a *function* which produces the format you
want ... but as Brian correctly points out, using the new date/time
classes in R 1.2 is much more convenient.

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