開始使用免費開始

在 data.frame 與 zoo 之間轉換

在處理真實世界的時間序列資料時,你常會從試算表與表格式資料匯入——也就是像資料框一樣的格式。將資料轉換成 zoo 物件後,你會更好地進行時間序列分析!

同樣地,把時間序列轉成資料框,能讓你用更通用的格式操作與匯出資料,以便在 R 以外的其他軟體與程式語言中讀取。

card_prices 時間序列(3 款交易卡每日平均價格的時間序列),以及 lubridatezooggplot2 套件都已為你載入。

本練習屬於課程

在 R 中操作時間序列資料

檢視課程

練習說明

  • card_prices zoo 物件強化(fortify)為資料框,指派給 cards_df

  • cards_df 的 3 個價格欄位相加,指派到新欄位 cards_df$total_price

  • cards_dfIndextotal_price 欄位建立新的時間序列 total_price_zoo

  • 使用 autoplot 繪製 total_price_zoo

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Fortify to data frame: cards_df
___ <- ___

# Add together the three price columns from cards_df
___ <- ___ + ___ + ___

# Create the total_price_zoo time series
___ <- ___

# Generate an autoplot of the new time series
___
編輯並執行程式碼