分開處理日期與時間欄位
當日期與時間位於不同欄位時,read.zoo() 能讓匯入變得很容易。index.column 引數可讓你指定包含索引資料的欄位名稱或編號。若日期與時間是標準格式(日期為 "%Y-%m-%d"、時間為 "%H:%M:%S"),你只需要這麼做就可以了。
在本練習中,你會使用 index.column 引數來指定檔案中的日期與時間欄位。你的工作目錄裡有一個名為 UNE.csv 的檔案,包含能源公司 Unron 的 5 分鐘 OHLC 資料。你仍會先用 read.csv() 找出日期與時間欄位的欄名。
本練習屬於課程
在 R 匯入與管理金融資料
練習說明
- 使用
read.csv()匯入UNE.csv的前 5 行。將輸出指定給une_data。 - 檢視
une_data的結構,並記下欄位名稱與位置。 - 使用
read.zoo()匯入UNE.csv,並將index.columns設為日期與時間欄位的「名稱」。將輸出指定給une_zoo。 - 檢視
une_zoo的前幾列。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Read data with read.csv
___ <- read.csv("___", nrows = ___)
# Look at the structure of une_data
# Specify Date and Time index column names
une_zoo <- read.zoo("UNE.csv", index.column = c("___", "___"), sep = "___", header = ___)
# Look at first few rows of data