问题
i have a variable that contains values about " the beginning of time interval expressed as the number of millisecond elapsed from the Unix Epoch on January 1st, 1970 at UTC." (according to data source metadata)
This is the head:
x$timeInt [1] 1.388068e+12 1.388075e+12 1.388096e+12 1.388051e+12 1.388051e+12 1.388072e+12
So i try to convert it as POSIXct
as.POSIXct(x$timeInt, origin = '01-01-1970',tz='UTC')
but i get this result
[1] "43987-03-01 05:20:00 UTC" "43987-05-23 13:20:00 UTC" "43988-01-28 13:20:00 UTC" "43986-08-25 17:20:00 UTC"
[5] "43986-08-25 17:20:00 UTC" "43987-04-25 18:40:00 UTC"
As you can see, the year is totally wrong. I tried using other formats in origin like "1970-01-01", but the result is the same. I know thata data is taken in december 2013.
回答1:
You have to take care, that this is in milliseconds, so:
x$timeInt <- x$timeInt/1000
And then one of the two approaches:
as.POSIXct(x$timeInt, origin = '1970-01-01',tz='UTC')
or
library(anytime)
anytime(x$timeInt)
#[1] "2013-12-26 15:26:40 CET" "2013-12-26 17:23:20 CET" "2013-12-26 23:13:20 CET" "2013-12-26 10:43:20 CET" "2013-12-26 10:43:20 CET"
#[6] "2013-12-26 16:33:20 CET"
来源:https://stackoverflow.com/questions/42817372/convert-numeric-variable-into-posixct