我正在从
下载数据集url = "http://robjhyndman.com/tsdldata/misc/kings.dat"
使用read.csv
有效,
read.csv(url, comment.char="#")
但是read.table
read.table(url, comment.char="#")
给出错误:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 2 did not have 8 elements
答案 0 :(得分:4)
如果你看一下这个文件的前几行,你会得到:
Age of Death of Successive Kings of England
#starting with William the Conqueror
#Source: McNeill, "Interactive Data Analysis"
60
43
67
使用read.table
时,默认的分隔符参数为sep=""
,帮助文件解释为:
sep: the field separator character. Values on each line of the
file are separated by this character. If 'sep = ""' (the
default for 'read.table') the separator is 'white space',
that is one or more spaces, tabs, newlines or carriage
returns.
因此第一行被视为八个值。当read.table
获得数值时,我们会收到错误:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 2 did not have 8 elements
read.csv
函数使用逗号作为分隔符,因此第一行只是一个值。