Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
762 views
in Technique[技术] by (71.8m points)

r - How to read date time from 2 columns with zoo?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...