导入由日期索引的csv的问题

问题描述 投票:1回答:2

我正在尝试导入带有OHLC数据的csv文件以与quantmod一起使用。我不断收到日期列的错误。

这是错误:

Error in read.zoo(file2, sep = ",", format = "%Y-%m-%d h:m:s.S", header = TRUE,  : index has 4706262 bad entries at data rows: 1 2 3...

我用来导入数据的内容:

zz <- read.zoo(file2, sep = ",",format="%Y-%m-%d h:m:s.S", 
header=TRUE,index.column=1,colClasses=c("character",rep("numeric",5)))
head(zz)

xx<- as.xts(zz)

我试着看看docs,但它没有帮助我。

这是我的csv文件中的一个示例

time,open,high,low,close,volume,
2005-01-02 10:29:00.0,1.356,1.356,1.356,1.356,1,
2005-01-02 10:38:00.0,1.356,1.356,1.356,1.356,1,
2005-01-02 10:51:00.0,1.3567,1.3567,1.3567,1.3567,1,
2005-01-02 10:52:00.0,1.3565,1.3565,1.3565,1.3565,1,
2005-01-02 10:55:00.0,1.3568,1.3568,1.3568,1.3568,1,
2005-01-02 10:57:00.0,1.3567,1.3567,1.3567,1.3567,1,
2005-01-02 11:04:00.0,1.3569,1.3569,1.3569,1.3569,1,
2005-01-02 11:07:00.0,1.357,1.357,1.3569,1.3569,2,
r csv xts
2个回答
1
投票

问题中的format(参见?strptime)和colClasses(参见?read.table)参数是错误的,并且那里指定的一些参数是不必要的。

1)以下较短的代码应该做。在它忽略点之后的部分时,但无论如何都是零,所以无关紧要。如果真实数据中某些行中的点后面有非零数字,那么请参阅上面提到的帮助文件,以获取在format中使用的正确百分比代码。

library(zoo)
read.zoo(text = Lines, header = TRUE, sep = ",", colClasses = c(X = "NULL"))

赠送:

                      open   high    low  close volume
2005-01-02 10:29:00 1.3560 1.3560 1.3560 1.3560      1
2005-01-02 10:38:00 1.3560 1.3560 1.3560 1.3560      1
2005-01-02 10:51:00 1.3567 1.3567 1.3567 1.3567      1
2005-01-02 10:52:00 1.3565 1.3565 1.3565 1.3565      1
2005-01-02 10:55:00 1.3568 1.3568 1.3568 1.3568      1
2005-01-02 10:57:00 1.3567 1.3567 1.3567 1.3567      1
2005-01-02 11:04:00 1.3569 1.3569 1.3569 1.3569      1
2005-01-02 11:07:00 1.3570 1.3570 1.3569 1.3569      2

(我们使用text = Lines保持这个自包含 - Lines在下面的注释中给出 - 你将用问题中的文件名替换它。)

1a)更短的将是以下。

read.csv.zoo(text = Lines, colClasses = c(X = "NULL"))

2)或者,这两步法特别简单:

DF <- read.csv(text = Lines)[-7]
read.zoo(DF)

3)

library(zoo)

read.zoo(text = Lines, read = function(...) read.csv(...)[-7])

注意:输入是:

Lines <- "
time,open,high,low,close,volume,
2005-01-02 10:29:00.0,1.356,1.356,1.356,1.356,1,
2005-01-02 10:38:00.0,1.356,1.356,1.356,1.356,1,
2005-01-02 10:51:00.0,1.3567,1.3567,1.3567,1.3567,1,
2005-01-02 10:52:00.0,1.3565,1.3565,1.3565,1.3565,1,
2005-01-02 10:55:00.0,1.3568,1.3568,1.3568,1.3568,1,
2005-01-02 10:57:00.0,1.3567,1.3567,1.3567,1.3567,1,
2005-01-02 11:04:00.0,1.3569,1.3569,1.3569,1.3569,1,
2005-01-02 11:07:00.0,1.357,1.357,1.3569,1.3569,2,"

更新:在最初发布时,这里的一些命令需要动物园的开发版本,但现在已经出来了所以它们都与CRAN上的常规动物园一起工作。


1
投票

假设您的数据位于名为‘testFile.csv’的文件中并位于您的工作目录中,您可以执行以下操作来获取xts-object的请求:

testFile <- read.csv("testFile.csv", header=TRUE, colClasses=c("character",rep("numeric",5),NULL))

>as.xts(testFile[,2:6],order.by = as.POSIXct(testFile[,1]))

                      open   high    low  close volume
2005-01-02 10:29:00 1.3560 1.3560 1.3560 1.3560      1
2005-01-02 10:38:00 1.3560 1.3560 1.3560 1.3560      1
2005-01-02 10:51:00 1.3567 1.3567 1.3567 1.3567      1
2005-01-02 10:52:00 1.3565 1.3565 1.3565 1.3565      1
2005-01-02 10:55:00 1.3568 1.3568 1.3568 1.3568      1
2005-01-02 10:57:00 1.3567 1.3567 1.3567 1.3567      1
2005-01-02 11:04:00 1.3569 1.3569 1.3569 1.3569      1
2005-01-02 11:07:00 1.3570 1.3570 1.3569 1.3569      2
© www.soinside.com 2019 - 2024. All rights reserved.