【发布时间】:2015-02-17 20:04:28
【问题描述】:
我的数据来自这个URL,结构如下:
93193KFAT FAT2013123016150015 NP [0000 ] 0.00 39999 29.791
93193KFAT FAT2013123016160016 NP [0000 ] 0.00 39999 29.791
93193KFAT FAT2013123016170017 NP [0000 ] 39999 29.791
93193KFAT FAT2013123016170017 NP [0000 ] 0.00 39999 29.791
所以如果你看到这个:
- 数据以空格分隔,
- 缺少一些列条目(即第 3 行中的 0.00)。
当我将它加载到 R 中时,它给了我以下错误:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 377 did not have 12 elements
你如何解决这个问题?这样我就可以直接从 URL 打开它而没有任何问题?
谢谢!
> read.fwf(ftp://ftp.ncdc.noaa.gov/pub/data/asos-onemin/6406-2013/64060KFAT201312.dat)
Error: unexpected '/' in "read.fwf(ftp:/"
trying URL 'ftp://ftp.ncdc.noaa.gov/pub/data/asos-onemin/6406- 2013/64060KFAT201312.dat'
using Synchronous WinInet calls
Error in download.file(url, downloadPath) :
cannot open URL 'ftp://ftp.ncdc.noaa.gov/pub/data/asos-onemin/6406-2013/64060KFAT201312.dat'
In addition: Warning message:
In download.file(url, downloadPath) : InternetOpenUrl failed: ''
Error in download.file(url, downloadPath) : unsupported URL scheme
1) 试试 url("....)。我得到错误:
Error in url("ftp.ncdc.noaa.gov/pub/data/asos-onemin/6406-2013/64060KFAT201312.dat") :
unsupported URL scheme
2) 我尝试使用 library(RCurl) 并执行:getURL("...)。我收到错误:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file [... and R shows the data in the url ]
【问题讨论】:
-
使用
utils::read.fwf -
我想读取(并可能写入)数据集,我使用 R Studio 的“工具”导入数据集,但我认为这不是最好的方法
-
要么是 fwf 格式,要么是制表符分隔的格式。
-
我尝试使用 read.fwf,但出现以下错误。我更新了问题以包含错误
-
在 ftp 目录的 README 文件中记录为 fwf,告诉您查看:www1.ncdc.noaa.gov/pub/data/documentlibrary/tddoc/td6406.pdf 请阅读 read.fwf 的帮助文件。你绝对需要一个宽度参数。您还绝对需要 URL 周围的引号。你需要阅读
?connection,其中描述了url函数。