read.zoo 以日期作为 R 中的索引

read.zoo with date as index in R

我有这样的 xts table

           Open     High      Low    Close       Volume
2011-09-13     5.80     6.00     5.65     5.97 5.837138e+01
2011-09-14     5.58     5.72     5.52     5.53 6.114598e+01
2011-09-15     5.12     5.24     5.00     5.13 8.014080e+01
2011-09-16     4.82     4.87     4.80     4.85 3.991401e+01

我通过 write.zoo

将此 table 写入 csv

但现在我想再读一遍 read.zoo 的 table,但是没用..

下面的代码

indata <- read.zoo(file = "H:/UsersData/test.csv", header=TRUE, index.column = 1, format="%Y/%m/%d", tz="CET", sep = ',')

仍然是错误

Error in read.zoo(file = "test.csv",  : 
  index has 2313 bad entries at data rows: 1 2 3 4 5 6 7 8 9 10 11 12

问题不清楚test.csv的内容到底是什么。没有给出生成它的代码,只使用了 write.zoo,并且没有给出文件内容,尽管它可能是问题中的第一个代码块?

在没有清晰的可重现输入定义的情况下,我们假设它是由最后注释中的代码可重现生成的。如果文件内容看起来与此不同,则需要相应地修改读取它的代码。

在下面的代码中,index = 0 表示输入将索引存储在行名中。

我们不必指定:

  • "Date" class 索引因为这是默认值
  • format因为yyyy-mm-dd也是默认值
  • header 因为数据字段比标题字段多一个所以它会自动假定第一行是 header

代码:

library(zoo)
read.zoo("test.csv", index = 0)

备注

假设生成的输入文件是这样的:

Lines <- '
           Open     High      Low    Close       Volume
2011-09-13     5.80     6.00     5.65     5.97 5.837138e+01
2011-09-14     5.58     5.72     5.52     5.53 6.114598e+01
2011-09-15     5.12     5.24     5.00     5.13 8.014080e+01
2011-09-16     4.82     4.87     4.80     4.85 3.991401e+01
'
cat(Lines, file = "test.csv")