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.

Lvp7
  • 59
  • 4
  • 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: https://stackoverflow.com/a/5963610/6574038 – jay.sf Feb 11 '21 at 09:35
  • Hi @jay.sf, thanks for pointing that out. I have edited my question accordingly. Hope this makes it more clear. – Lvp7 Feb 15 '21 at 08:41
  • I'm sure you overlooked the reproducible data section in the link. Just provide your integer64 vector using `dput`. – jay.sf Feb 15 '21 at 08:47

1 Answers1

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"
jangorecki
  • 16,384
  • 4
  • 79
  • 160