gpt4 book ai didi

r - 将数字时间转换为 R 中的日期时间 POSIXct 格式

转载 作者:行者123 更新时间:2023-12-01 11:38:39 27 4
gpt4 key购买 nike

我有一个数据框,其中包含已读入 R 的日期时间列。时间值显示为数字时间,如下面的数据示例所示。我想将这些转换为 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 <- 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))

关于r - 将数字时间转换为 R 中的日期时间 POSIXct 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24509002/

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