我正在使用read.table()
从网页中获取数据。数据表有两列,来自NIST。以下是我的代码,包括要预览数据的URL:
options(digits = 12)
theURL <- "https://www.itl.nist.gov/div898/strd/anova/AtmWtAg.dat"
AgData <- read.table(theURL, header = TRUE , skip = 59)
# Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
# line 1 did not have 3 elements
AgData$Instrument = as.factor(AgData$Instrument)
fitAgData = aov(AgWt ~ Instrument, data=AgData)
我已将错误消息作为注释插入到发生错误的代码中。
有关堆栈交换的其他答案似乎是在处理导致此错误的缺失值。该站点上的数据是完整的,所以我不确定是什么导致了错误。
到目前为止,我已经摆弄skip =
值;插入列名作为read.table
参数;并将fill = TRUE
添加到read.table
中,后者产生了一个包含三列的数据表,其中一列包含NA
值。由于表中有名称,因此我使用了header=TRUE
参数。
不知何故read.table()
认为有三列,但我看不出有两种方法可以解释。
在跳过行之后,我看到了文件的开头,如下所示:
Data:
Instrument AgWt
1 107.8681568
1 107.8681465
1 107.8681572
1 107.8681785
实际上,数据看起来像这样:
Data: Instrument AgWt
1 107.8681568
1 107.8681465
1 107.8681572
1 107.8681785
答案 0 :(得分:1)
它将“ Data:”标识为第一列的名称。因此,它将扫描标题并识别3列,然后转到数据,仅看到2。请尝试跳过该行,然后手动命名数据。