zoo::yearmon
在處理以月份與季度為基礎的時間序列資料時,常需要使用 zoo 套件中的 yearmon 與 yearqtr 類別。這麼做可以讓你在觀測的具體日期未知或不必要時,仍能妥善表示資料。
在這個練習中,你會使用銷售資料向量 monthly_data,以及每筆觀測日期索引 date_index_month,其中每次觀測都發生在每月的第 1 天。
上述資料集,以及 ggplot2、zoo 和 lubridate 套件都已提供給你使用。
本練習屬於課程
在 R 中操作時間序列資料
練習說明
將
date_index_month轉換為yearmon類別並指定給index_yearmon。使用
monthly_data的數值與index_yearmon作為索引,建立zoo物件monthly_sales。使用 autoplot 繪製
monthly_sales,並將x 軸標籤設為"Month"、y 軸標籤設為"Sales ($100k)",標題設為"Corporate Sales Data"。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a year-monthly index
___
# Create a zoo object from the year-month index and monthly data
___
# Autoplot with titles and axis labels
___