開始使用免費開始

使用 merge 將不規則索引轉為規則索引

上一個練習教你如何建立具有規則時間索引、寬度為 0 的 xts 物件。你可以利用這個零寬物件,將不規則的 xts 物件規則化。

規則化後的序列通常會出現遺漏值(NA),因為不規則資料在規則索引中的每個觀測點並不一定都有數值。這個練習會教你在使用 merge() 合併兩個序列時,如何處理這些遺漏值。

上一個練習建立的 irregular_xtsregular_xts 已經在你的工作區中。

本練習屬於課程

在 R 匯入與管理金融資料

檢視課程

練習說明

  • 使用 merge()irregular_xtsregular_xts 合併為 merged_xts
  • head() 檢視 merged_xts 的前幾列。
  • 使用 merge()fill 參數,將 NA 以最後觀測值往前填補(na.locf),並將結果存為 merged_filled_xts
  • head 檢視 merged_filled_xts 的前幾列。

動手互動練習

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

# Merge irregular_xts and regular_xts


# Look at the first few rows of merged_xts


# Use the fill argument to fill NA with their previous value


# Look at the first few rows of merged_filled_xts
編輯並執行程式碼