【问题标题】:Load incomplete .dat file into R将不完整的 .dat 文件加载到 R 中
【发布时间】: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 

所以如果你看到这个:

  1. 数据以空格分隔,
  2. 缺少一些列条目(即第 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 函数。

标签: r ftp


【解决方案1】:

类似这样的事情:

a <- read.fwf("ftp://ftp.ncdc.noaa.gov/pub/data/asos-onemin/6406-2013/64060KFAT201312.dat", 
              widths=c(9, 20, 2, 3, 9, 5, 6, 7))

【讨论】:

  • 我得到...文件中的错误(文件,“rt”):无法打开连接,是因为网站吗?
  • 呵呵,一定是windows什么的。试试这个:a ftp.ncdc.noaa.gov/pub/data/asos-onemin/6406-2013/…), widths=c(9, 20, 2, 3, 9, 5, 6, 7))
  • 几乎,如果我保存了数据,您的代码将非常有效。让我更新问题,我无法使 url 工作......
猜你喜欢
  • 2015-11-27
  • 2017-08-29
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 2019-09-05
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多