2

Similar Q&A everywhere, but none helped me to overcome the following error (I am trying to convert unix time to date-time format):

> cur196$time
integer64
  [1] 1566204590000 1566204585000 1566204580000 1566204570000 1566204560000 1566204550000 1566204531000 1566204525000 1566204521000 1566204501000
 [11] 1566204495000 1566204491000 1566204481000 1566204464000 1566204461000 1566204451000 1566204441000 1566204434000 1566204431000 1566204420000
 [21] ...
>   cur196$time <- as.POSIXct(cur196$time, origin = "1970-01-01", tz = "GMT")
Error in as.POSIXct.default(cur196$time, origin = "1970-01-01", tz = "GMT") : 
  do not know how to convert 'cur196$time' to class “POSIXct”

EDIT:

> dput(head(cur196$time))
structure(c(7.73807882277875e-312, 7.73807879807547e-312, 7.73807877337218e-312, 
7.73807872396562e-312, 7.73807867455905e-312, 7.73807862515249e-312
), class = "integer64")

EDIT 2:

@zx8754 thank you very much for changing the title and thus pointing out the real problem - unix time stamps are in miliseconds and thus to large for conversion.

meolic
  • 1,177
  • 2
  • 15
  • 41
  • 1
    can you add `dput(head(cur196$time))` ? – Ronak Shah Aug 19 '19 at 09:41
  • Possible duplicate [Convert UNIX epoch to Date object](https://stackoverflow.com/questions/13456241/convert-unix-epoch-to-date-object) – deepseefan Aug 19 '19 at 10:12
  • OK, it could be a duplicate of https://stackoverflow.com/questions/17007023/convert-unix-timestamp-column-to-day-of-week-in-r . But, I do not know how to adapt the proposed solution to my example. – meolic Aug 19 '19 at 11:14
  • 1
    Glad to help. Please remove the "SOLUTION" from your question and post it as answer below: `"Your Answer"`. – zx8754 Aug 19 '19 at 12:58
  • By the way, your solution didn't work for dput example data provided in the question. – zx8754 Aug 19 '19 at 13:03
  • By mistake, I used to mix up original data and those already divided by 1000. I changed this to keep the question usable. – meolic Aug 19 '19 at 13:42

1 Answers1

2

The problem is that your data has the class integer64 from the bit64 package. You need to convert it to a normal integer with as.integer(), while the bit64 package is loaded. Then you can use as.POSIXct() on it.

But probably you should look into how you imported the data and why it is saved that way in the first place. Does it need to be a 64-bit integer?

shs
  • 3,683
  • 1
  • 6
  • 34
  • 1
    Correct! After I realised that my data are in miliseconds I also find out that they must be divided by 1000 (seems that this automatically creates normal integers). Like this: cur196$time <- as.POSIXct(cur196$time/1000, origin = "1970-01-01", tz = "GMT") – meolic Aug 19 '19 at 13:40