2

我有以下文件

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

我想将此文件读入 R 中的 zoo(或 xts)对象。此文件是作为 xts 对象创建并使用保存的write.zoo(as.zoo(xts_object), path, sep = "\t"),现在我正在尝试使用zoo_object <- read.zoo(path, sep = "\t", header=TRUE, format="%Y-%m-%d %H:%M:%S"). 但是,我收到以下警告

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

当我zoo_object在控制台中输入以显示其内容时,我得到:

           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

您可以在其中看到时间不包含在行索引中。我假设我可以将时间字段转换为索引并修复我的问题,但我也假设我在读取这个文件(或者可能是写入)时做错了,但是经过一整天的搜索,我不知道是什么。任何人都可以提供任何见解吗?

dput(zoo_object)读后

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

(请注意,测试所需的对象是传递给 write.zoo 的对象,而不是最终对象。)

默认情况下(看起来)使用的日期时间函数read.zooas.Date,而我猜它会是as.POSIXct. 您可以通过以下方式强制执行所需的行为:

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

请注意,我稍微更改了您的格式,因为在编辑器中查看文本输出时,它似乎是一列,其中“T”作为日期和时间文本之间的分隔符。

于 2013-01-10T21:11:31.633 回答