【发布时间】:2018-05-19 10:47:54
【问题描述】:
我有一个要转换为时间序列的数据框。问题是每个日期我都有多个产品。好像
Name_Article Week Num_Any Spending Unit_Price
1 Product_A 1 2016 196.05 3.376000
2 Product_B 1 2016 377.04 1.004867
3 Product_A 2 2016 2979.40 3.376000
4 Product_C 2 2016 353.44 3.034444
5 Product_D 2 2016 160.99 0.653621
6 Product_E 2 2016 950.22 1.441164
7 Product_A 3 2016 196.05 3.376000
8 Product_B 3 2016 377.04 1.004867
9 Product_D 3 2016 2979.40 0.653621
10 Product_E 3 2016 353.44 1.441164
11 Product_A 4 2016 160.99 3.376000
12 Product_B 4 2016 950.22 1.441164
我知道使用每周时间序列不是最佳选择,但我别无选择。我的想法是得到类似的东西
Week Spending.A UnitPrice.A Spending.B UnitPrice.B Spending.C UnitPrice.C ...
1 196.05 3.376000 377.04 1.004867 0.00 3.034444
2 2979.40 3.376000 0.00 1.004867 353.44 3.034444
3 120.05 3.376000 377.04 1.004867 0.00 3.950000
4 160.99 3.500000 950.22 1.441164 ...
我无法理解 tydir 函数 gather() 和 spread()。任何帮助将不胜感激!
如果您想知道,所有这些的目标是执行分层预测,但在开始之前,我需要对我的数据进行结构化。
非常感谢!
【问题讨论】:
-
您的数据是长格式的 - 我认为您应该保持这种格式。如您所见,采用宽幅格式会使建模更加困难。
标签: r dataframe time-series hierarchical