[R] Reformatting dates using chron

David James dj at research.bell-labs.com
Fri Jan 12 19:38:56 CET 2001


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


> 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 Criswell
Professor of Finance
Graduate School, Bangkok University

------------- End Forwarded Message -------------

-------------- next part --------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2920.0" name=GENERATOR>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Hello All:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</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><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # Compute 
sequence of dates for weekdays only (exclude weekends)</FONT></DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 
dts &lt;- seq.dates("01/02/98", "01/10/01", by="day")<BR>&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; dts &lt;- dts[!is.weekend(dts)]</FONT></DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; # Reformat 
dates as YYYY/MM/DD</FONT></DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 
m &lt;- as.numeric(months(dts))<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp; m &lt;- ifelse(m&lt;10,paste(0,m,sep=""),m)</FONT></DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 
d &lt;- as.numeric(days(dts))<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp; d &lt;- ifelse(d&lt;10,paste(0,d,sep=""),d)</FONT></DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; 
dte &lt;- paste(years(dts),m,d,sep="/")</FONT></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>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Best wishes,</FONT></DIV>
<DIV><FONT face=Arial size=2>ANDREW</FONT></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>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><BR>&nbsp;</DIV></FONT></BODY></HTML>

More information about the R-help mailing list