Converting Excel numer data into R date values using read.xslx

时间:2015-11-12 11:29:33

标签: r date

I am reading an excel file with the read.xslx function from package openxlsx. Everything is being read correctly, except one of the variables is a numeric value, 42006.33361, which represents the date/time stamp 02/01/2015 08:00:24, which is dd/mm/YYYY HH:MM:SS format. R maintains the variables numeric value when reading from the Excel file, but when I attempt to convert the value to a date/time variable in R, using as.POSIX..., I am unable to convert the data to the correct date/time stamp. I am using the following code:

> janfile7$timeStamp<-as.POSIXct(janfile7$Date,origin="1970-01-01")

which generates

> print(janfile7$timeStamp[1:25])
 [1] "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST"
 [6] "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST"
[11] "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST"
[16] "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST"
[21] "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST" "1970-01-01 06:40:06 EST"

Or, the following code

> janfile7$timeStamp<-as.Date(janfile7$Date,origin="1899-12-30")

Which generates the following...

> print(janfile7$timeStamp[1:25])
 [1] "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02"
[12] "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02" "2015-01-02"
[23] "2015-01-02" "2015-01-02" "2015-01-02"

What is the correct "origin" value in the "as....." functions to use to convert the Excel numeric data back to the correct date/time stamp value in R?

Sorry for the naive question. I've been searching for a few days, and nothing is getting data back to the correct value.

0 个答案:

没有答案