> con=url("http://www.nwrfc.noaa.gov/esp/esp2csv.cgi?id=slkw1")
> table(count.fields(con, sep=","))
1 25521
2 1
所以只有一个字段的两行...事实证明,跳过两行可以让数据读取成功。
> con=url("http://www.nwrfc.noaa.gov/esp/esp2csv.cgi?id=slkw1")
> temp <- scan(file=con, skip=2, what="a", sep=",")
# paged through the scan results
> table(grepl("<br />", temp))
FALSE TRUE
25478 43
> con=url("http://www.nwrfc.noaa.gov/esp/esp2csv.cgi?id=slkw1")
> temp <- readLines(con=con)
> temp2=gsub("<br />", "\n", temp)
# Knew that the first two lines were HTML junk.
> temp3 = read.table( text=temp2[3], sep= ",", fill =TRUE, header=TRUE)
> str(temp3)
'data.frame': 43 obs. of 581 variables:
$ X.pre.STA.ID: Factor w/ 1 level "SLKW1": 1 1 1 1 1 1 1 1 1 1 ...
$ START.YEAR : int 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 ...
$ X9.1 : int -999 -999 -999 -999 -999 -999 -999 -999 -999 -999 ...
$ X9.2 : int -999 -999 -999 -999 -999 -999 -999 -999 -999 -999 ...
$ X9.3 : int -999 -999 -999 -999 -999 -999 -999 -999 -999 -999 ...
$ X9.4 : int -999 -999 -999 -999 -999 -999 -999 -999 -999 -999 ...
$ X9.5 : int -999 -999 -999 -999 -999 -999 -999 -999 -999 -999 ...
$ X9.6 : int -999 -999 -999 -999 -999 -999 -999 -999 -999 -999 ...
snipped output There is better data below
$ X9.18 : int 41 41 41 41 41 41 41 41 41 41 ...
$ X9.19 : int 41 41 41 41 41 41 41 41 41 41 ...
$ X9.20 : int 40 40 40 40 40 40 40 40 40 40 ...
$ X9.21 : int 39 39 39 39 39 39 39 39 39 39 ...
$ X9.22 : int 39 39 39 39 39 39 39 39 39 39 ...
$ X9.23 : int 39 39 39 39 39 39 39 39 39 39 ...
$ X9.24 : int 38 38 38 38 38 38 38 38 38 38 ...
$ X9.25 : int 38 38 38 38 38 38 38 38 38 38 ...
$ X9.26 : int 38 38 38 38 38 38 38 38 38 38 ...
$ X9.27 : int 38 38 38 38 38 38 38 38 38 38 ...
$ X9.28 : int 48 53 106 49 89 48 94 117 49 49 ...
$ X9.29 : int 57 45 436 40 403 38 145 294 40 40 ...
$ X9.30 : int 259 34 1270 37 1622 33 80 432 37 37 ...
最后一列被强制转换为取决于 read.table 设置的因子或字符,因为最后一行中的最后一个元素是
> temp3[43, 581]
[1] "-999</pre></body>"
有几种处理方法。选一个。并不重要,因为整列都是 -999。