Re: [R] Handling dates

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Fri 19 Aug 2005 - 21:35:45 EST

Göran Broström <gb@stat.umu.se> writes:

> I have a problem with some functions handling dates, in packages 'date' and
> 'survival' (they seem to be identical). For instance, from the documentation,
>
> --------------------
> mdy.date {survival}
>
> R Documentation
>
> Convert to Julian Dates
>
> Description
>
> Given a month, day, and year, returns the number of days since January 1, 1960.
> Usage
>
> mdy.date(month, day, year, nineteen = TRUE, fillday = FALSE,
> fillmonth = FALSE)
> ----------------------------
> but
>
> > library(survival)
> > mdy.date(12, 1, 1977)
> [1] 1Dec77
>
> Similar strange results appear in other date-related functions. I plan to
> write functions that converts, eg, "1977-01-31" to the real number 1977.084
> and back. What function in R does what 'mdy.date' claims to do?

mdy.date does ...

> dput(mdy.date(12, 1, 1977))
structure(6544, class = "date")

> mdy.date(1, 31, 1977)/365.2425+1960 # or /365.25 if you really want 1977.084
[1] 1977.085

But with recent R's, as.Date is a better bet:

> c(as.Date("1977-01-31") - as.Date("0000-01-01"))/365.2425
[1] 1977.084

(notice that you need the c() to avoid the nonsense of

> (as.Date("1977-01-31") - as.Date("0000-01-01"))/365.2425
Time difference of 1977.084 days

)

-- 
   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@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri Aug 19 21:42:52 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:54 EST