将Unix时间戳转换为日期时间 [英] Convert Unix timestamp into datetime

查看:117
本文介绍了将Unix时间戳转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框

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

I am unable to convert the UNIX timestamp into datetime using the following methods I tried

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




as.POSIXlt.character(x,tz,...)中的错误:
字符串不是标准的明确格式

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"

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

I'm not sure what I'm doing incorrectly here.

推荐答案

您必须从在使用as.POSIXct之前将字符转换为数字的因素。该函数期望整数作为Unixtime

You have to convert from Factor to Character To Number before using as.POSIXct. The function is expecting an integer as a Unixtime

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

这篇关于将Unix时间戳转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆