1

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.

3
  • 1
    Are you using as.POSIXct(x, origin="1970-01-01") or really as.POSIXct.default(x, origin="1970-01-01")? Generally you should ask reproducible questions on Stack Overflow, please read: stackoverflow.com/a/5963610/6574038
    – jay.sf
    Feb 11, 2021 at 9:35
  • Hi @jay.sf, thanks for pointing that out. I have edited my question accordingly. Hope this makes it more clear.
    – Lvp7
    Feb 15, 2021 at 8:41
  • I'm sure you overlooked the reproducible data section in the link. Just provide your integer64 vector using dput.
    – jay.sf
    Feb 15, 2021 at 8:47

1 Answer 1

1

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.