2014-10-10 86 views
0

有沒有人有關於如何將其轉換爲xts R對象的想法?如何將csv轉換爲R的xts對象?

#format: symbol,interval,date,time,open,high,low,close,, 
# cat test.csv 
AD.F,5,20141008,093500,0.8736,0.8738,0.8732,0.8733,0,0 
AD.F,5,20141008,094000,0.8732,0.8736,0.8731,0.8735,0,0 
AD.F,5,20141008,094500,0.8736,0.8737,0.8733,0.8737,0,0 
AD.F,5,20141008,095000,0.8736,0.8747,0.8736,0.8746,0,0 
AD.F,5,20141008,095500,0.8746,0.8748,0.8742,0.8743,0,0 
AD.F,5,20141008,100000,0.8744,0.875,0.8742,0.8747,0,0 
AD.F,5,20141008,100500,0.8747,0.8747,0.874,0.8745,0,0 
AD.F,5,20141008,101000,0.8744,0.8744,0.874,0.8743,0,0 
AD.F,5,20141008,101500,0.8744,0.875,0.8744,0.875,0,0 
AD.F,5,20141008,102000,0.8751,0.8752,0.8746,0.8751,0,0 

我試過read.csv(),但我有時間格式問題(read.csv轉換時間數,例如,從「091500」到91500)。

回答

2

我們可以指定使用colClasses將日期和時間列視爲字符。這也處理索引。如果有在第1列的多個值,然後將每個分成單獨的列:

library(zoo) 
z <- read.zoo("input.csv", sep = ",", split = 1, index = 3:4, tz = "", 
     format = "%Y%m%d %H%M%S", colClasses = rep(c(NA, "character", NA), c(2, 2, 6))) 

這可以通過as.xts(z)被轉換爲XTS。有關更多信息,請參閱read.zoo和動物園小插圖Reading Data in zoo上的幫助文件。

或者,使用相同的colClassesread.table