将R中的数字时间转换为datetime POSIXct格式 [英] Converting numeric time to datetime POSIXct format in R

查看:4534
本文介绍了将R中的数字时间转换为datetime POSIXct格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中包含已经被读入R的datetime列。时间值显示为数字时间,如下面的数据示例所示。我想将它们转换成datetime POSIXct或POSIXlt格式,以便查看日期和时间。

  tdat < c(974424L,974430L,974436L,974442L,974448L,974454L,974460L,974466L,974472L,
974478L,974484L,974490L,974496L,974502L,974508L,974514L,974520L,974526L,
974532L,974538L)

974424 应该等于 00:00:00 01/03/2011 ,但不知道数值的起始时间(即1970-01-01以下使用不起作用)。我已经尝试使用下面的命令来实现这一点,并花费了时间尝试获得 as.POXISct 来工作,但是我还没有找到一个解决方案最终得到了一个POSIXct对象的NAs或者结束了晦涩的datetime值)。



尝试将数字时间转换为日期时间:

  datetime<  -  as.POSIXct(strptime(time,format =%d /%m /%Y%H:%M:%S))
datetime< - as.POSIXct(as.numeric(time),origin ='1970-01-01')

我确信这是一件简单的事情。任何帮助都将得到很大的收获。谢谢!

解决方案

根据您想要的时区,尝试其中一个:

  t.gmt < -  as.POSIXct(3600 *(tdat  -  974424),origin ='2011-03-01',tz =GMT)

t.local< - as.POSIXct(format(t.gmt))


I have a data frame containing what should be a datetime column that has been read into R. The time values are appearing as numeric time as seen in the below data example. I would like to convert these into datetime POSIXct or POSIXlt format, so that date and time can be viewed.

tdat <- c(974424L, 974430L, 974436L, 974442L, 974448L, 974454L, 974460L, 974466L, 974472L,
          974478L, 974484L, 974490L, 974496L, 974502L, 974508L, 974514L, 974520L, 974526L,
          974532L,974538L)

974424 should equate to 00:00:00 01/03/2011, but the do not know the origin time of the numeric values (i.e. 1970-01-01 used below does not work). I have tried using commands such as the below to achieve this and have spent time trying to get as.POXISct to work, but I haven’t found a solution (i.e. I either end up with a POSIXct object of NAs or end up with obscure datetime values).

Attempts to convert numeric time to datetime:

datetime <- as.POSIXct(strptime(time, format = "%d/%m/%Y %H:%M:%S"))
datetime <- as.POSIXct(as.numeric(time), origin='1970-01-01') 

I am sure that this is a simple thing to do. Any help would be greatly received. Thanks!

解决方案

Try one of these depending on which time zone you want:

t.gmt <- as.POSIXct(3600 * (tdat - 974424), origin = '2011-03-01', tz = "GMT") 

t.local <- as.POSIXct(format(t.gmt))

这篇关于将R中的数字时间转换为datetime POSIXct格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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