Hi,
You may tailor the format to be anything by specifying your own
formatting function, e.g.,
format(dts) <- myfun
for example,
dts <- seq.dates("01/02/98", "01/10/01",
by="day")
dts <- dts[!is.weekend(dts)]
format(dts) <- function(d){
two.digits <- function(x) paste(ifelse(x<10, "0",
""), x, sep="")
a <- month.day.year(as.integer(d), origin(d))
paste(a$year, two.digits(a$month), two.digits(a$day), sep="/")
}
If you don't want to put the entire function in the object, simply
put its name (R symbol)
myformat <- function(d){
... same as before ...
}
format(dts) <- as.name("myformat")
Thus,
> dts[1:5]
[1] 1998/01/02 1998/01/05 1998/01/06 1998/01/07
1998/01/08> dts[1:5] + 3
[1] 1998/01/05 1998/01/08 1998/01/09 1998/01/10
1998/01/11> format(dts[1:5])
[1] "1998/01/02" "1998/01/05" "1998/01/06"
"1998/01/07" "1998/01/08"
David A. James
Statistics Research, Room 2C-253 Phone: (908) 582-3082
Bell Labs, Lucent Technologies Fax: (908) 582-3340
Murray Hill, NJ 09794-0636
------------- Begin Forwarded Message -------------
From: "Andrew Criswell" <andy at arcriswell.com>
To: <R-help at stat.math.ethz.ch>
Subject: [R] Reformatting dates using chron
Date: Fri, 12 Jan 2001 22:25:09 +0700
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6700
Hello All:
Thank you, Prof. Ripley for your suggestion. My newbiesque workaround to the
problem was this:
# Compute sequence of dates for weekdays only (exclude weekends)
dts <- seq.dates("01/02/98", "01/10/01",
by="day")
dts <- dts[!is.weekend(dts)]
# Reformat dates as YYYY/MM/DD
m <- as.numeric(months(dts))
m <- ifelse(m<10,paste(0,m,sep=""),m)
d <- as.numeric(days(dts))
d <- ifelse(d<10,paste(0,d,sep=""),d)
dte <- paste(years(dts),m,d,sep="/")
There must be a more elegant and aesthetically pleasing alternative than this.
Best wishes,
ANDREW
Andrew Criswell
Professor of Finance
Graduate School, Bangkok University
------------- End Forwarded Message -------------
-------------- next part --------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=iso-8859-1"
http-equiv=Content-Type>
<META content="MSHTML 5.00.2920.0" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Hello All:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Thank you, Prof. Ripley for your
suggestion. My
newbiesque workaround to the problem was this:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>
# Compute
sequence of dates for weekdays only (exclude weekends)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>
dts <- seq.dates("01/02/98", "01/10/01",
by="day")<BR>
dts <-
dts[!is.weekend(dts)]</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>
# Reformat
dates as YYYY/MM/DD</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>
m <- as.numeric(months(dts))<BR>
m <-
ifelse(m<10,paste(0,m,sep=""),m)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>
d <- as.numeric(days(dts))<BR>
d <-
ifelse(d<10,paste(0,d,sep=""),d)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>
dte <- paste(years(dts),m,d,sep="/")</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>There must be a more elegant and
aesthetically
pleasing alternative than this.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Best wishes,</FONT></DIV>
<DIV><FONT face=Arial size=2>ANDREW</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Andrew
Criswell</FONT></DIV>
<DIV><FONT face=Arial size=2>Professor of
Finance</FONT></DIV>
<DIV><FONT face=Arial size=2>Graduate School, Bangkok
University</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial
size=2><BR> </DIV></FONT></BODY></HTML>