gpt4 book ai didi

r - 将不均匀数据时间 [时间戳] 系列转换为常规时间系列 : R

转载 作者:行者123 更新时间:2023-12-05 06:40:03 24 4
gpt4 key购买 nike

我有一个数据框“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转换为常规时间序列以进行这样的预测(Forecasting time series data)但我不知道如何将时间戳值系列添加到 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,然后将新的 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 值,因为您的原始时间序列是不规则时间序列。

更新:

关于r - 将不均匀数据时间 [时间戳] 系列转换为常规时间系列 : R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43650420/

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