我正在尝试从ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/ghcnd-stations.txt中读取气候站信息。但是,由于第一行没有完全填充(缺少最后两个列),并且第五列包含空格,所以我无法完成以下阅读:
fread('ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/ghcnd-stations.txt',sep=)
它返回错误消息:
Expected sep (' ') but new line, EOF (or other non printing character) ends
field 5 when detecting types from point 0: AGE00135039 35.7297 0.6500
50.0 ORAN-HOPITAL MILITAIRE
如何在读取此txt文件时正确应用fread?谢谢!
您为什么不尝试read.fwf
使用utils软件包中的函数呢?列宽在readme.txt文件中给出(请参阅第IV节)。
IV. FORMAT OF "ghcnd-stations.txt"
------------------------------
Variable Columns Type
------------------------------
ID 1-11 Character
LATITUDE 13-20 Real
LONGITUDE 22-30 Real
ELEVATION 32-37 Real
STATE 39-40 Character
NAME 42-71 Character
GSN FLAG 73-75 Character
HCN/CRN FLAG 77-79 Character
WMO ID 81-85 Character
------------------------------
但是,以下尝试返回错误:
data <- read.fwf("ghcnd-stations.txt", widths = c(11,9,10,7,3,31,4,4,6))
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
line 25383 did not have 7 elements
检查第25,383行发现了错误的原因。
> x <- readLines("ghcnd-stations.txt", 25383)
> tail(x, 1)
[1] "CA002100627 60.8167 -137.7333 846.0 YT HAINES APPS #4 "
因此,可以通过包含comment.char
参数来避免这种情况,将值从默认值(#)更改为其他值,可能只是null。
data <- read.fwf("ghcnd-stations.txt", widths = c(11,9,10,7,3,31,4,4,6), comment.char="")
只需要大约20秒。不需要fread
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句