Question

I have a csv file contains minutes exchange rate

<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
...

Trying to do below but not working for me. How to read date time from 2 columns?

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

above code generated error : index has 5 bad entries at data rows: 1 2 3 4 5

Était-ce utile?

La solution

You need to specify colClasses to keep the leading zeros on the third column, and to remove the first column (since you can't have both numbers and characters in a zoo object). Note that the default action if multiple index columns are specified is to paste them together with a space between them.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top