我一定是误解了read.csv在R中的工作原理。我已经阅读了帮助文件,但仍然不理解包含以下内容的csv文件:
40900,-,-,-,241.75,0
40905,244,245.79,241.25,244,22114
40906,244,246.79,243.6,245.5,18024
40907,246,248.5,246,247,60859使用:euk<-data.matrix(read.csv("path\to\csv.csv"))读入R
结果是(使用tail)生成以下内容:
Date Open High Low Close Volume
[2713,] 15329 490 404 369 240.75 62763
[2714,] 15330 495 409 378 242.50 127534
[2715,] 15331 1 1 1 241.75 0
[2716,] 15336 504 425 385 244.00 22114
[2717,] 15337 504 432 396 245.50 18024
[2718,] 15338 512 442 405 247.00 60859这肯定是一些我不明白的显而易见的事情。请善待你的回复,我正在努力学习。
谢谢!
发布于 2013-04-27 02:14:42
问题不在于read.csv,而在于data.matrix。read.csv将其中包含字符的任何列作为因子导入。数据集第一行中的“-”是字符,因此该列将转换为因子。现在,将read.csv的结果传递给data.matrix,正如帮助所述,它用它的内部代码替换了因子的级别。
基本上,在将data.frame传递给data.matrix之前,您需要确保数据列是数字的。
这应该适用于您的情况(假设只有‘-’字符):
euk <- data.matrix(read.csv("path/to/csv.csv", na.strings = "-", colClasses = 'numeric'))发布于 2013-04-27 02:10:40
我不是R专家,但你可以考虑使用scan(),例如:
> data = scan("foo.csv", what = list(x = numeric(), y = numeric()), sep = ",")其中,foo.csv有两列,x和y,并以逗号分隔。我希望这能有所帮助。
发布于 2013-04-27 02:54:48
我剪切/粘贴了你的数据,把它放在一个文件中,我用'R‘得到了这个
> c<-data.matrix(read.csv("c:/DOCUME~1/Philip/LOCALS~1/Temp/x.csv",header=F))
> c
V1 V2 V3 V4 V5 V6
[1,] 40900 1 1 1 241.75 0
[2,] 40905 2 2 2 244.00 22114
[3,] 40906 2 3 3 245.50 18024
[4,] 40907 3 4 4 247.00 60859
> 在你的数据文件中必须有更多的内容,比如标题行的数据。并且您显示的输出似乎从第2713行开始。我会检查:
The format of the header line, or get rid of it and add it manually later.
That each row has exactly 6 values.
The the filename uses forward slashes and has no embedded spaces
(use the 8.3 representation as shown in my filename).此外,如果从MS Excel生成csv文件,则日期的内部表示形式为数字。
https://stackoverflow.com/questions/16242584
复制相似问题