2014-02-10 55 views
2

我有一個CSV文件包含分鐘匯率如何從動物園的2欄中讀取日期時間?

<TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL> 
EURUSD,20110103,000000,1.3353,1.3354,1.3353,1.3354,4 
EURUSD,20110103,000100,1.3355,1.3356,1.3355,1.3356,4 
EURUSD,20110103,000200,1.3355,1.3356,1.3350,1.3350,4 
EURUSD,20110103,000300,1.3349,1.3349,1.3348,1.3348,4 
EURUSD,20110103,000400,1.3347,1.3348,1.3347,1.3348,4 
... 

嘗試做以下,但不適合我的工作。如何從2列讀取日期時間?

rate <- read.zoo("data.csv",sep=",",tz="",header=T, format='%Y%m%d %H%M%S', index = 2:3) 

上面的代碼生成的錯誤:index has 5 bad entries at data rows: 1 2 3 4 5

回答

3

你需要指定colClasses保持前導零的第三列,並刪除第一列(因爲你不能同時擁有數字和字符一個動物園對象)。請注意,如果指定了多個索引列,則默認操作是將它們粘貼在一起,並在它們之間留出空格。

Lines <- "<TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL> 
EURUSD,20110103,000000,1.3353,1.3354,1.3353,1.3354,4 
EURUSD,20110103,000100,1.3355,1.3356,1.3355,1.3356,4 
EURUSD,20110103,000200,1.3355,1.3356,1.3350,1.3350,4 
EURUSD,20110103,000300,1.3349,1.3349,1.3348,1.3348,4 
EURUSD,20110103,000400,1.3347,1.3348,1.3347,1.3348,4 
" 

rate <- read.zoo(text=Lines, sep=",", header=TRUE, 
    index.column=1:2, format="%Y%m%d %H%M%S", tz="", 
    colClasses = rep(c("NULL", "character", "numeric"), c(1, 2, 5))) 

MODIFIED:Simplified。

相關問題