2

I have the following file

"Index" "time"  "open"  "high"  "low"   "close" "numEvents" "volume"
2013-01-09 14:30:00 "2013-01-09T14:30:00.000"   "110.8500"  "110.8500"  "110.8000"  "110.8000"  " 57"   "32059"
2013-01-09 14:31:00 "2013-01-09T14:31:00.000"   "110.7950"  "110.8140"  "110.7950"  "110.8140"  "  2"   " 1088"
2013-01-09 14:32:00 "2013-01-09T14:32:00.000"   "110.8290"  "110.8300"  "110.8290"  "110.8299"  "  5"   "  967"
2013-01-09 14:33:00 "2013-01-09T14:33:00.000"   "110.8268"  "110.8400"  "110.8268"  "110.8360"  "  8"   " 2834"
2013-01-09 14:34:00 "2013-01-09T14:34:00.000"   "110.8400"  "110.8400"  "110.8200"  "110.8200"  " 33"   " 6400"

I want to read this file into a zoo (or xts) object in R. This file was created as an xts object and saved using write.zoo(as.zoo(xts_object), path, sep = "\t") and now I am trying to read it in using zoo_object <- read.zoo(path, sep = "\t", header=TRUE, format="%Y-%m-%d %H:%M:%S"). However, I get the following warning

Warning message:
In zoo(rval3, ix) :
  some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique

And when I type zoo_object into the console to show its contents i get:

           time                    open     high    low      close    numEvents volume
2013-01-09 2013-01-09T14:30:00.000 110.8500 110.850 110.8000 110.8000 57        32059 
2013-01-09 2013-01-09T14:31:00.000 110.7950 110.814 110.7950 110.8140  2         1088 
2013-01-09 2013-01-09T14:32:00.000 110.8290 110.830 110.8290 110.8299  5          967 
2013-01-09 2013-01-09T14:33:00.000 110.8268 110.840 110.8268 110.8360  8         2834 
2013-01-09 2013-01-09T14:34:00.000 110.8400 110.840 110.8200 110.8200 33         6400

where you can see that the time is not included in the row index. I assume I can convert the time field into the index and fix my problems, but I also assume I am doing something wrong in reading this file (or maybe writing), but after search all day I have no idea what. Can anyone offer any insight?

dput(zoo_object) after read

dput(zoo_object)
structure(c("2013-01-09T14:30:00.000", "2013-01-09T14:31:00.000", 
"2013-01-09T14:32:00.000", "2013-01-09T14:33:00.000", "2013-01-09T14:34:00.000", 
"110.8500", "110.7950", "110.8290", "110.8268", "110.8400", "110.850", 
"110.814", "110.830", "110.840", "110.840", "110.8000", "110.7950", 
"110.8290", "110.8268", "110.8200", "110.8000", "110.8140", "110.8299", 
"110.8360", "110.8200", "57", " 2", " 5", " 8", "33", "32059", 
" 1088", "  967", " 2834", " 6400"), .Dim = c(5L, 7L), .Dimnames = list(
    NULL, c("time", "open", "high", "low", "close", "numEvents", 
    "volume")), index = structure(c(15714, 15714, 15714, 15714, 
 15714), class = "Date"), class = "zoo")
4

1 に答える 1

4

(Please note that the object that was desired for testing was the one passed to write.zoo, not the final object.)

By default (it appears) the date-time function used by read.zoo is as.Date while I would have guessed it would be as.POSIXct. You can force the desired behavior with:

zoo_object <- read.zoo("~/test", index.column=2, sep = "\t", 
                        header=TRUE, format="%Y-%m-%dT%H:%M:%S", FUN=as.POSIXct)

Note that I changed your format slightly because looking at the text output in an editor, it appeared that the was a single column with "T" as a separator between the Date and time text.

于 2013-01-10T21:11:31.633 に答える