![](/img/trans.png)
[英]Convert data frame with epoch timestamps to time-series with milliseconds in R
[英]Convert uneven Data Time [timestamps] series to regular time series : R
我有一個數據框“ gg”,看起來像這樣:
> head(gg)
timestamps value
1 2017-04-25 16:52:00 -0.4120000
2 2017-04-25 16:53:00 -0.4526667
3 2017-04-25 16:54:00 -0.4586667
4 2017-04-25 16:55:00 -0.4606667
5 2017-04-25 16:56:00 -0.5053333
6 2017-04-25 16:57:00 -0.5066667
我需要將其繪制為時間序列數據以進行預測。 步驟如下:
1) gg$timestamps <- as.POSIXct(gg$timestamps, format = "%Y-%m-%d %H-%M-%S") #changing "Timestamps" column 'factor' to 'as.POSIXct'.
2) gg.ts <- xts(x=gg$value, order.by = gg$timestamps) #converting the dataframe to time series (Non Regular Time series)
現在,我希望將gg.ts
轉換為常規時間序列以進行這樣的預測( 預測時間序列數據 ),但是我不知道如何將時間戳值序列添加到ts
函數。 當我嘗試它拋出錯誤:
> gg.xts <- ts(gg.ts, frequency = '1', start = c(2017-04-25 16:52:00,171))
Error: unexpected numeric constant in "gg.xts <- ts(gg.ts, frequency = '1', start = c(2017-04-25 16"
我的整個問題都在這里清楚列出。 https://stackoverflow.com/questions/43627826/plotting-time-series-data-r-plotly-timestamp-values請幫助我。 謝謝
在進行所有操作之前,您需要確保您擁有正確的數據類。 確保gg$timestamps
采用POSIXct格式或日期格式(以您的偏好為准)
gg$timestamps<- as.POSIXct(strftime(gg$timestamps,format = "%Y-%m-%d %H:%M:%S"))
您需要做的就是根據您的特定間隔使用時間序列值生成一個新的data.frame,然后將其與gg
合並
tstamp <- data.frame(x = seq(head(gg$timestamps,1),tail(gg$timestamps,1),by = "sec"))
res <-merge(tstamp, gg, by.x="x",by.y="timestamps",all.x = TRUE)
xts(res$value,order.by = res$x) # you create your xts time series this way.
注意:由於原始時間序列是不規則的時間序列,因此其中將包含一些NA值。
更新:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.