我正在与R中的一个令人困惑的错误作斗争。我有一个包含许多列的数据集。当我阅读它时,我看到错误消息,如"Error in scan(file,what,nmax,sep,dec,quote,skip,nlines,na.strings,“)。
> data=read.table(file("D:/R/data.txt"), header=T)
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 1 did not have 26 elements因此,我尝试了下面的方法:
> data=read.table(file("D:/R/data.txt"), fill=T, header=F)
> head(data)
V1 V2 V3 V4 V5 V6 V7 V8
1 Year Na NH4 K Mg Ca Cl NO3
2 2009 40.9311 89.072 65.4617 48.5279 3091.9586 52.782114 173.414523
3 2009 19.8996 75.3554 22.219 11.8805 223.5573 28.51786 219.186039好吧,不管怎样,R读了我的数据。然后我想要绘图,所以我删除了第一行,这实际上是我的标题。
> data<-data[-c(1), ]
> head(data)
V1 V2 V3 V4 V5 V6 V7 V8
2 2009 40.9311 89.072 65.4617 48.5279 3091.9586 52.782114 173.414523
> boxplot(V20~factor(V1), data=data, main="")
Error in boxplot.default(split(mf[[response]], mf[-response]), ...) :
adding class "factor" to an invalid object
> a<-aggregate(V20~V1, data=data, mean)
There were 50 or more warnings (use warnings() to see the first 50)最初,我只想读取我的数据集,并将每一列作为year (first colulmn)的函数进行聚合。但我看到了错误消息,它可能会要求我将数据转换为数据框。那我就不知所措了。有没有办法用header读取我的数据,然后简单地绘制一个框图并聚合数据?
发布于 2014-03-03 22:01:45
可能是标题和数据的分隔符不同。
将此作为答案发布,结果证明是正确的解决方案:)
https://stackoverflow.com/questions/22144341
复制相似问题