2010-10-21 219 views
4

我有一個data.frame,包含幾個月的歷史數據。我想在這個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) 

我堅持的是什麼如何添加日期順序月的休息,包括從預測正確的價值觀。我想生成一些看起來像這樣的東西,將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 

任何幫助表示讚賞。謝謝。

回答

2

這是幾乎可以肯定不是這樣做的理想方式,但它會得到你在那裏:

#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) 

這是拆分爲多個步驟,比你需要的,但它更容易閱讀的方式。希望這可以幫助。

+0

馬特,那得到的日期。預測(結果$收入)部分呢?我應該做一些預測< - 預測(結果$收入),然後fc < - rbind(results.fc,forecast)? – analyticsPierce 2010-10-22 01:42:23

+0

我想我明白你的意思了。我可以用data.frame函數末尾的預測(結果$收入)代替收入= NA。但是,我收到一個錯誤,表示有不同數量的行。 – analyticsPierce 2010-10-22 01:59:36

+0

我得到了與預測相等的行(結果$收入,44)。當我運行這個時,它會掛上forecast()函數返回5個變量;預測Lo 80,Hi 80,Lo 95和Hi 95.我不知道如何選擇預測組件。 – analyticsPierce 2010-10-22 03:14:54