Here you go.
First, your data. Note the superfluous first columns first throws off the indices used in your code. Easily corrected though.
R> TestData
X Date Time Open High Low Close Volume
1 1 07/01/1998 06:31 114.06 114.06 114.00 114.00 257600
2 2 07/01/1998 06:34 114.11 114.13 114.06 114.06 24400
3 3 07/01/1998 06:35 114.06 114.13 114.06 114.13 2500
4 4 07/01/1998 06:36 114.09 114.09 114.03 114.03 900
5 5 07/01/1998 06:38 114.09 114.09 114.06 114.06 3000
6 6 07/01/1998 06:39 114.06 114.13 114.06 114.13 16700
R>
Much worse is that you forced date and time to a factor type. Not a good idea:
R> sapply(TestData, class)
X Date Time Open High Low Close Volume
"integer" "factor" "factor" "numeric" "numeric" "numeric" "numeric" "integer"
R>
So when we work with, we first convert to character, then paste, then parse and lastly convert to as.POSIXct
which is a type that xts
is rather happy with.
R> x <- xts(TestData[,4:8],
+ order.by=as.POSIXct(strptime(paste(as.character(TestData[,2]),
+ as.character(TestData[,3])),
+ "%m/%d/%Y %H:%M")))
R>
Et voila:
R> myxts
Open High Low Close Volume
1998-07-01 06:31:00 114.06 114.06 114.00 114.00 257600
1998-07-01 06:34:00 114.11 114.13 114.06 114.06 24400
1998-07-01 06:35:00 114.06 114.13 114.06 114.13 2500
1998-07-01 06:36:00 114.09 114.09 114.03 114.03 900
1998-07-01 06:38:00 114.09 114.09 114.06 114.06 3000
1998-07-01 06:39:00 114.06 114.13 114.06 114.13 16700
R>
But you really should spend some time with the extensive documentation for xts
and zoo
.