r - 如何将日期和预测添加到 data.frame?

标签 r

我有一个 data.frame,其中包含几个月的历史数据。我想在这个 data.frame 的末尾添加一个预测。

例如,我可以运行以下命令来创建一个简单的示例 data.frame。

months <- c("2010-10-17","2010-10-18","2010-10-19","2010-10-20")
revenue <- c(10000,11000,10500,9500)

results <- data.frame(months,revenue)

并运行此函数以生成预测。
forecast(results$revenue)

我所困扰的是如何为 10 月的剩余时间添加日期序列并包含预测中的正确值。我想生成看起来像这样的东西,将 30 天的预测添加到历史数据中。
> print(results)
       months revenue
1  2010-10-17   10000
2  2010-10-18   11000
3  2010-10-19   10500
4  2010-10-20    9500
5  2010-10-21   10250
6  2010-10-22   10250
7  2010-10-23   10250
8  2010-10-24   10250
9  2010-10-25   10250
10 2010-10-26   10250
11 2010-10-27   10250
12 2010-10-28   10250
13 2010-10-29   10250
14 2010-10-30   10250
15 2010-10-31   10250

任何帮助表示赞赏。谢谢。

最佳答案

这几乎肯定不是执行此操作的理想方式,但它会让您到达那里:

#Get the sequence from a date to another date
date.seq <- as.Date("2010-10-17"):as.Date("2010-10-31")

#Unfortunately, add.dates is numeric - but can easily be converted back to
#dates if you know the origin (which you can find at ?Date)
new.dates <- data.frame(months = as.Date(date.seq, origin = "1970-01-01"),
                        revenue = NA)

#Slap it on to your existing dates
results.fc <- rbind(results, new.dates)

这被分成比你需要的更多的步骤,但这样更容易阅读。希望这可以帮助。

关于r - 如何将日期和预测添加到 data.frame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3992634/

相关文章:

Rcpp:为什么我不能在我定义的包中运行该函数?

r - R 中的 princomp 用于 PCA - 输出实例中的分数变量。

r - + 符号出现在 R 的控制台中

r - 如何指定 by( ) 或相关的 apply( ) 函数中使用的 FUN

r - R中的邻居函数

r - 读取大型 Excel xlsx 文件的最快方法?并行化与否?

r - 使用管道运算符将数据转换为 data.frame

r - 无法使用 spark_read_csv() 将 csv 读入 Spark

r - 并行大矩阵乘法

r - 相当于 R 中的 C getch()