Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert R Date into Excel numeric serial date?

Tags:

r

excel

Excel stores dates internally as floating-point serial date. We know how to convert it into R - using as.Date(var2, origin="1899-12-30", tz='UTC').

The question is: how to convert it back into Excel? I tried all the obvious things, but I still get a mismatch.

like image 444
Adam Ryczkowski Avatar asked May 15 '17 16:05

Adam Ryczkowski


People also ask

How do I convert a date to a serial number in Excel?

Convert date to serial number with DATEVALUE Also, you can apply the DATEVALUE function to convert date cells to serial numbers. Select a blank cell which will place the serial number, type this formula =DATEVALUE("8/8/2017"), press Enter key. Now the date in formula will be displayed as serial number.

How do I change a date to numeric date in R?

You can use the as. Date( ) function to convert character data to dates. The format is as. Date(x, "format"), where x is the character data and format gives the appropriate format.

How do I convert a date to an integer in R?

Simply format the Date in that way, then call as. integer on the result. If your date isn't a Date, and is a character string: either convert it to a Date first, or remove the "-" before calling as.


1 Answers

One approach is, for example for the date '2017-05-15' :

d0 <- as.Date(0, origin="1899-12-30", tz='UTC')
d1 <- as.Date('2017-05-15')

d <- as.numeric(d1-d0)
# or in one line
d <- as.numeric(as.Date('2017-05-15') -as.Date(0, origin="1899-12-30", tz='UTC'))
print(d)
[1] 42870

In excel the date '2017-05-15' gives the same number when expressed as numeric serial date.

like image 143
COLO Avatar answered Nov 14 '22 21:11

COLO