開始使用免費開始

編碼並繪製紅襪隊資料

在你探索並操作了航班、天氣與經濟的資料後,你的客戶想要全面掌握情況。很自然地,他們希望你蒐集波士頓主要職業球隊的資料:Boston Red Sox(棒球)、New England Patriots(美式足球)、Boston Bruins(冰球)、以及 Boston Celtics(籃球)。在本章中,你將整理這些球隊在 2010 到 2015 年間所有比賽的賽程與結果。這是進一步練習時間序列資料處理的絕佳機會!

作為開始,你已彙整 2010 到 2015 年間 Boston Red Sox 的比賽資料。在本練習中,你會先探索資料,將其編碼為 xts,並繪製一段時間內的趨勢。redsox 資料框已可在你的工作區使用。

本練習屬於課程

個案研究:在 R 中分析城市時間序列資料

檢視課程

練習說明

  • 使用 summary() 檢視 redsox 資料的一些摘要統計。留意日期欄位,並評估是否存在需要處理的遺漏值(NA)。
  • 確認 redsox 資料可轉成 xts 後,先用 as.Date()date 欄位編碼為時間型別物件。
  • 使用 as.xts()redsox 資料轉為 xts,記得用 order.by 指定 date 欄位排序。同時移除日期欄位(使用 [, -1] 表示法),以確保 xts 物件為數值型。
  • 使用 plot.zoo() 隨時間繪製紅襪隊得分(boston_score)與對手得分(opponent_score)。從這些圖中你能看出哪些趨勢?

動手互動練習

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

# View summary information about your redsox data


# Convert the date column to a time-based format
redsox$date <- as.Date(___$___)

# Convert your red sox data to xts
redsox_xts <- as.xts(___[,-1], order.by = ___$___)

# Plot the Red Sox score and the opponent score over time
plot.zoo(___[, c("___", "___")])
編輯並執行程式碼