“索引在数据行中有错误条目"读取CSV时出错 [英] "Index has bad entries at data rows" error when reading CSV

查看:34
本文介绍了“索引在数据行中有错误条目"读取CSV时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .csv 文件,其中包含以下格式的股票价格

I have a .csv file containing stock prices in the below format

Date    Open    High    Low Close
3/7/2014 10:15  228.28  236.58  228.23  236.18
3/7/2014 11:15  236.23  241.27  236.09  241.14
3/7/2014 12:15  241.1   246.12  241.1   242.96
3/7/2014 13:15  242.84  243.92  242     242.32

当我运行以下脚本时:

test <- as.xts(read.zoo(mytest.csv', header=T, fill=T, index.column = 1, row.names=NULL,stringsAsFactors = FALSE, sep="",format="%m/%d/%y %H:%M", tz=""))

显示以下错误

read.zoo 出错("mydata.csv", header = T, fill = T, index.column =1, : 索引在数据行有错误条目:1 2 3 4 5 6 7 8

Error in read.zoo("mydata.csv", header = T, fill = T, index.column = 1, : index has bad entries at data rows:1 2 3 4 5 6 7 8

推荐答案

我不确定您从哪里获得 as.xts 函数,但我看到您需要获取数据的两个小修复进入 R.

I'm not sure where you get the as.xts function from but I see two small fixes you need to get the data into R.

首先你应该为时间添加一个额外的变量名,因为 read.zoo 函数使用与 read.table 相同的分隔符,所以如果你想让它使用一个标题,那么你需要为每一列命名.

First you should add an extra variable name for the time since the read.zoo function uses the same delimiter as read.table so if you want it to work with a header then you need a name for each column.

Date     Time   Open    High    Low     Close
3/7/2014 10:15  228.28  236.58  228.23  236.18
3/7/2014 11:15  236.23  241.27  236.09  241.14
3/7/2014 12:15  241.1   246.12  241.1   242.96
3/7/2014 13:15  242.84  243.92  242     242.32

其次,您可以在上面的文件中读取如下

Second, you can read in the file above as follows

library(zoo)
indata <- read.zoo("mydata.csv", header=TRUE, index.column = 1:2, format="%m/%d/%Y %H:%M", tz="CET")

请注意,由于日期/时间分为两列,因此给出了两个索引.另外,我认为日期/时间转换需要 tz 才能工作.无论如何,上面给出了

Note that two indices are given since the date/time is split over two columns. Also, I think a tz is needed for the date/time conversion to work. In any case the above gives

> indata
                      Open   High    Low  Close
2014-03-07 10:15:00 228.28 236.58 228.23 236.18
2014-03-07 11:15:00 236.23 241.27 236.09 241.14
2014-03-07 12:15:00 241.10 246.12 241.10 242.96
2014-03-07 13:15:00 242.84 243.92 242.00 242.32

可以传递给其他 R 函数.

which can be passed to other R functions.

这篇关于“索引在数据行中有错误条目"读取CSV时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆