[R] Convert number to Date

Rolf Turner r.turner at auckland.ac.nz
Thu Mar 25 23:08:52 CET 2010



Kind of off the thread a bit, but when I do:

> as.Date(40182)

I ***do not*** get "2080-01-06".  Instead I get an error:

Error in as.Date.numeric(40182) : 'origin' must be supplied

Am I the only user who gets picked on in this way, or does it
happen to others as well?  The help on as.Date() clearly specifies
that "origin" must be supplied.  So how come Anna got the result that
she did?

> sessionInfo()
R version 2.10.1 (2009-12-14) 
i386-apple-darwin8.11.1 

locale:
[1] en_NZ.UTF-8/en_NZ.UTF-8/C/C/en_NZ.UTF-8/en_NZ.UTF-8

attached base packages:
[1] datasets  utils     stats     graphics  grDevices methods   base     

other attached packages:
[1] misc_0.0-12    fortunes_1.3-7 MASS_7.3-4

	cheers,

		Rolf Turner 

On 26/03/2010, at 9:47 AM, Joshua Wiley wrote:

> Dear Anna,
> 
> Rolf's explanation not withstanding, it has to do with differences in
> how R and Excel treat dates.  If you use
> 
> as.Date(40182, origin="1899-12-30")
> 
> you will get the same date as Excel.  You can look at:
> 
> http://office.microsoft.com/training/training.aspx?AssetID=RC102786151033&CTT=6&Origin=RP102786121033
> 
> For an official guide on how Excel dates are calculated.  Apparently,
> Excel's calendar starts on 1 January 1900.  Why one has to use an
> origin of 30 December 1899 in R...well, I suspect that gets back to
> Rolf's explanation.
> 
> 
> HTH,
> 
> Josh
> 
> On Thu, Mar 25, 2010 at 1:33 PM, anna <lippelanna24 at hotmail.com> wrote:
>> 
>> Hello, I have a date value in excel: 1/4/2010 which in number format gives me
>> 40182. When I read this with read.xls from R I get same 40182 so what I do
>> is that I use the as.Date() function but here is what the as.Date() function
>> returns me:
>>> as.Date(40182)
>> [1] "2080-01-06"
>> Why don't I get the same thing as in excel?
>> Thanks!

######################################################################
Attention: 
This e-mail message is privileged and confidential. If you are not the 
intended recipient please delete the message and notify the sender. 
Any views or opinions presented are solely those of the author.

This e-mail has been scanned and cleared by MailMarshal 
www.marshalsoftware.com
######################################################################



More information about the R-help mailing list