[R] Date formats

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Dec 30 11:47:26 CET 2007


Gabor Grothendieck wrote:
> Read the warning in ?ifelse
Yep.

And, yes, it is annoying that ifelse() strips attributes, including 
class, but it is one of those things that have been in the S languages 
"forever", and nobody really wants to  mess with. The fundamental issue 
is that you need the result to be able to hold values from both of the 
"yes" and the "no" arguments and there is no guarantee that that is 
possible outside of the R base types.

You'd like to have things like these "work"

d <- as.Date(c("1994-3-4", "1996-3-1"))

ifelse(d > "1996-1-1", "1996-1-1", d)

ifelse(d <= "1996-1-1", d, "1996-1-1")

in the sense that the result is a Date object, but once you start 
thinking about the details of how it _might_ work, you find that things 
aren't all that simple. If there was a general mechanism for coercion 
between classes, then maybe it could be done, but there isn't any.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list