I am reading an xlsx file using read_excel
from the readxl
package in R. My column has a string in it so readxl
gets it as a string. However the date times get transformed into a numeric before and then given as a string. Using as.Date
I can correctly get the Date portion of the datetime but I loose the timestamp. Using as.POSIXct
with different origins does not give the right timestamp. Here is an example:
Time as it appears in Excel: 28/09/2016 14:47
It is read by read_excel as a string: "42641.6163310185"
The following fail to give 28/09/2016 14:47
> as.POSIXct(as.numeric("42641.6163310185"), origin = "1899-12-30")
[1] "1899-12-30 11:50:42.616331 GMT"
> as.POSIXct(as.numeric("42641.6163310185"), origin = "2016-09-28")
[1] "2016-09-28 12:50:41.616331 BST"
What is the correct transformation and is some information getting lost by the read_excel
function?
EDIT:
From data difference in as.POSIXct
in R with Excel The transformation is *86400
which I wasn't aware of. The following works:
> as.POSIXct(42641.6163310185*86400, origin = "1899-12-30", tz="UTC")
[1] "2016-09-28 14:47:30 UTC"