[英]Convert Unix timestamp into datetime
I have the following data frame我有以下数据框
> 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 ...
I am unable to convert the UNIX timestamp into datetime using the following methods I tried我无法使用我尝试过的以下方法将 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
as.POSIXlt.character(x, tz, ...) 中的错误:字符串不是标准的明确格式
> 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"
I'm not sure what I'm doing incorrectly here.我不确定我在这里做错了什么。
You have to convert from Factor to Character To Number before using as.POSIXct.在使用 as.POSIXct 之前,您必须将 Factor 转换为 Character To Number。 The function is expecting an integer as a Unixtime
该函数需要一个整数作为 Unixtime
head(as.POSIXct(as.numeric(as.character(try$time)), origin="1970-01-01", tz="GMT"))
这是因为 epoch 不是数字,尝试将其转换为数字,它就像奇迹一样工作!
try$time <- as.POSIXct(as.numeric(try$time), origin = '1970-01-01', tz = 'GMT')
Try尝试
head(as.POSIXct(as.integer(as.numeric(as.character(try$time)) / 1000.0),
origin='1970-01-01', tz="GMT"))
What about关于什么
library("anytime")
anytime(try$time)
you can also get the date from the timestamps anydate(try$time)
您还可以从时间戳中获取日期
anydate(try$time)
and if you want UTC, you can do anytime(try$time, asUTC = TRUE)
如果你想要 UTC,你可以
anytime(try$time, asUTC = TRUE)
做anytime(try$time, asUTC = TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.