【问题标题】:Convert numeric (integer64 class) UNIX timestamp to date time class将数字(integer64 类)UNIX 时间戳转换为日期时间类
【发布时间】:2019-12-24 12:23:56
【问题描述】:

到处都有类似的问答,但没有一个帮助我克服以下错误 (我正在尝试将 unix 时间转换为日期时间格式):

> 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”

编辑:

> 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")

编辑 2:

@zx8754 非常感谢您更改标题并指出真正的问题 - unix 时间戳以毫秒为单位,因此对于转换来说很大。

【问题讨论】:

  • 可以加dput(head(cur196$time))吗?
  • 好的,它可能是 stackoverflow.com/questions/17007023/… 的副本。但是,我不知道如何使建议的解决方案适应我的示例。
  • 很高兴为您提供帮助。请从您的问题中删除“解决方案”并将其发布为以下答案:"Your Answer"
  • 顺便说一句,您的解决方案不适用于问题中提供的 dput 示例数据。

标签: r dataframe datetime unix-timestamp integer64


【解决方案1】:

问题是您的数据具有来自bit64 包的integer64 类。您需要使用as.integer() 将其转换为普通整数,同时加载bit64 包。然后你可以在上面使用as.POSIXct()

但也许您应该首先了解您是如何导入数据的,以及为什么以这种方式保存数据。需要64位整数吗?

【讨论】:

  • 正确!在我意识到我的数据以毫秒为单位后,我还发现它们必须除以 1000(似乎这会自动创建普通整数)。像这样: cur196$time
猜你喜欢
  • 1970-01-01
  • 2020-10-19
  • 2015-02-09
  • 1970-01-01
  • 2020-12-23
  • 2016-04-24
  • 2012-07-04
  • 1970-01-01
相关资源
最近更新 更多