Reputation: 59
I'm trying to convert a column from my hadoop data containing UNIX values (integer) to POSIXct format in R. I am using the following code:
hadoop$time <- as.POSIXct(hadoop$time, origin="1970-01-01")
However, when I use as.POSIXct I get the following error:
Error in as.POSIXct.default(hadoop$time, origin="1970-01-01") : do not know how to convert 'hadoop$time' to class "POSIXct"
A sample of my column time values is this:
integer64
[1] 1606851081 1606851075 1606851065 1606850993 1606850976 1606823547
If I put one of these values through the Epoch date converter it returns a valid date time. I have tried multiple solutions posted on here such as using the "as_datetime" function or the "anytime" package. Sample code:
hadoop$time <- anytime(hadoop$time)
hadoop$time <- as_datetime(hadoop$time)
However these attempts have also been unsuccessful. Not sure what I am doing wrong here, hope somebody can help me out.
Some added context: I am running this in a notebook in Databricks.
Upvotes: 1
Views: 1521
Reputation: 16697
There is no reproducible code so the answer will not provide working example as well. Converting UNIX epoch time to POSIXct is just changing its class attribute. Assuming origin 1970-01-01.
Therefore whatever class of UNIX epoch you have, just coerce it to numeric, and then as.POSIXct(num, origin="1970-01-01")
.
int64_epoch = bit64::as.integer64(1606851081)
num_epoch = as.numeric(int64_epoch)
as.POSIXct(num_epoch, origin="1970-01-01")
#[1] "2020-12-01 21:31:21 EET"
Upvotes: 1