R:将时间戳转换为(尽可能短的)整数



编辑1:我认为一个可能的解决方案是计算自开始日期以来经过的15分钟间隔数。如果有人对此有想法,请站出来。感谢

正如标题所说,我正在寻找一种方法,将时间戳变成尽可能小的整数。

情况说明:

我正在与";面板AR";。我有T>N个面板数据包含不同的时间戳,如下所示(总共300000行(:

df$timestamp[1]
[1] "2013-08-01 00:15:00 UTC"

class(df$timestamp)
[1] "POSIXct" "POSIXt" 

我正在使用panelAR,因此需要将时间戳作为整数。我不能简单地使用";作为整数";因为我会达到整数的最大长度,结果只有NA。这是我第一次尝试解决这个问题:

df$timestamp <- as.numeric(gsub("[: -]", "" , df$timestamp, perl=TRUE))
Subtract the numbers starting at te 3rd position (Because "20" is irrelevant) and stop before the 2nd last position (Because they all end at 00 seconds)
(I need shorter integers in order to not hit the max level of integers in R)
df$timestamp <- substr(df$timestamp, 3, nchar(df$timestamp)-2)
#Save as integer
df$timestamp <- as.integer(df$timestamp)
#Result
df$timestamp[1]
1308010015

这允许panelAR使用它,但数字似乎太大了。当我尝试用它运行回归时,我得到以下错误消息:"不能分配大小为1052.2GB的矢量";

我正在寻找一种方法,将这些时间戳转换为(尽可能小的(整数,以便使用panelAR。

非常感谢您的帮助。

您得到的这个大数字对应于自1970-01-01 00:00:00以来经过的秒数。你的时间戳有固定的间隔吗?如果是的话,比方说,每15分钟你可以把所有整数除以900,这可能会有所帮助。

另一种选择是选择你的最早日期,并从其他中减去

#generate some dates:
a <- as.POSIXct("2013-01-01 00:00:00 UTC")
b <- as.POSIXct("2013-08-01 00:15:00 UTC")
series <- seq(a,b, by = 'min')
#calculate the difference (result are integers/seconds)
integer <- as.numeric(series - min(series)) 

如果你仍然有记忆力问题,我可能会把两者结合起来。

我设法解决了主要问题。由于这仍然会导致内存错误,我认为这源于观察次数和panelAR计算事物的方式。我将就这件事单独提出一个问题。

我使用

df$timestampnew <- as.integer(difftime(df$timestamp, "2013-01-01 00:00:00", units = "min")/15)

以获取整数,该整数计算自某个日期以来经过的15分钟间隔数。

最新更新