gpt4 book ai didi

r - 将 Unix 时间戳转换为日期时间

转载 作者:行者123 更新时间:2023-12-02 03:51:15 25 4
gpt4 key购买 nike

我有以下数据框

> 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 时间戳转换为日期时间

> 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 之前,您必须将 Factor 转换为 Character To Number。该函数需要一个 Unixtime 整数

head(as.POSIXct(as.numeric(as.character(try$time)), origin="1970-01-01", tz="GMT"))

关于r - 将 Unix 时间戳转换为日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27408131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com