[R] Numeric to Date

Achim Zeileis Achim.Zeileis at uibk.ac.at
Wed Mar 10 23:03:17 CET 2010


On Wed, 10 Mar 2010, GULATI, BRIJESH (Global Markets FF&O NY) wrote:

> Is there easy way to convert numeric to date?
>
> For example:
>
> If I have,
>
> 	a = as.Date("2010-03-04")
>
> And, if run
>
>> as.numeric(a)
> [1] 14672
>
> Is there any function that I can use to convert 14672 back to the date.

You can use as.Date(). In base R, you additionally need to specify
   origin = "1970-01-01"
because that is what is used in R. If you load the package "zoo" then the 
default is set automatically:

R> a <- as.Date("2010-03-04")
R> a
[1] "2010-03-04"
R> b <- as.numeric(a)
R> b
[1] 14672
R> as.Date(b)
Error in as.Date.numeric(b) : 'origin' must be supplied
R> as.Date(b, origin = "1970-01-01")
[1] "2010-03-04"
R> library("zoo")
R> as.Date(b)
[1] "2010-03-04"

hth,
Z

> Thx in advance,
> Brijesh
>
> --------------------------------------------------------------------------
> This message w/attachments (message) may be privileged, ...{{dropped:30}}
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list