[R-SIG-Finance] Simple Date Formatting

Robert Sams robert at sanctumfi.com
Thu Jan 7 12:57:49 CET 2010


Ralph, as you want to store a "date" in a column of a matrix object, you
can't format a Date class as it will be coerced to class numeric. 

How about this solution?

> intDate <- function(x){
+   y <- as.POSIXlt(as.Date(x))
+   ((y$year + 1900) * 10000) + ((y$mon + 1) * 100) + y$mday
+ }
> 
> as.Date.intDate <- function(x, ...){
+   year <- trunc(x / 10000)
+   month <- trunc(x / 100) - year * 100
+   day <- x - trunc(x / 100) * 100
+   as.Date(ISOdate(year, month, day))
+ }
> 
> (x <- intDate("2009-07-10"))
[1] 20090710
> as.Date.intDate(x)
[1] "2009-07-10"
>

Robert
-----Original Message-----
From: r-sig-finance-bounces at stat.math.ethz.ch
[mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of R. Vince
Sent: 03 January 2010 19:01
To: r-sig-finance at stat.math.ethz.ch
Subject: [R-SIG-Finance] Simple Date Formatting

Suppose I have a matrix, X, whose leftmost column is a date in the
format:

2009-07-10

how do I alter it such that it is formatted as:

20090710

without using string manipulations? Surely there mist be a common means
for 
doing this? Also, how can I remove the rightmost column of a matrix
prior to 
doing a write.table() ? Thanks so much -RVince

_______________________________________________
R-SIG-Finance at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions
should go.



More information about the R-SIG-Finance mailing list