加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

将Unix时间戳转换为datetime

发布时间:2020-12-15 18:25:14 所属栏目:安全 来源:网络整理
导读:我有以下数据框 head(try) creates time1 128.29508 14173920723 236.98361 14173920727 98.45902 14173920729 157.44068 141739213110 227.38333 141739213111 242.03390 1417392131 str(try)'data.frame': 102968 obs. of 2 variables: $creates: num 128.
我有以下数据框
> head(try)
     creates       time
1  128.29508 1417392072
3  236.98361 1417392072
7   98.45902 1417392072
9  157.44068 1417392131
10 227.38333 1417392131
11 242.03390 1417392131

> str(try)
'data.frame':   102968 obs. of  2 variables:
 $creates: num  128.3 237 98.5 157.4 227.4 ...
 $time   : Factor w/ 26418 levels "1417392071","1417392072",..: 2 2 2 3 3 3 3 3 5 5 ...

我无法使用我尝试的以下方法将UNIX时间戳转换为datetime

> head(as.POSIXlt(as.numeric(try$time),origin="1970-01-01",tz="GMT"))
[1] "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:03 UTC" "1970-01-01 00:00:03 UTC"
[6] "1970-01-01 00:00:03 UTC"

> head(as.POSIXct(as.character(try$time),tz="GMT"))

Error in as.POSIXlt.character(x,tz,…) :
character string is not in a standard unambiguous format

> head(as.POSIXlt(as.POSIXct(as.vector(as.numeric(try$time)),origin="1970-01-01")))
[1] "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:02 UTC" "1970-01-01 00:00:03 UTC" "1970-01-01 00:00:03 UTC"
[6] "1970-01-01 00:00:03 UTC"

我不确定我在这里做错了什么.

在使用as.POSIXct之前,您必须从因子转换为字符到数字.该函数期望一个整数作为Unixtime
head(as.POSIXct(as.numeric(as.character(try$time)),tz="GMT"))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读