如何读取以下示例中的数据?(我的实际文件类似于ftp://ftp.aoml.noaa.gov/hrd/pub/hwind/Operational/2012/AL182012/1030/0730/AL182012_1030_0730.gz,格式为http://www.aoml.noaa.gov/hrd/Storm_pages/grid.html--它们看起来像Fortran隐含循环写入)。我的问题是文件中有多个标题和向量,每行的值不同。对于.gz文件,扫描似乎从开头开始,而我希望读取逐步解析文件。
这样做有点效果,但我还需要计算跳过的值:
This is a headerline with a name.
The fourth line has the number of elements in the first vector,
and the next vector is encoded similarly
7
1 2 3
4 5 6
7
8
1 2 3
4 5 6
7 8
这不像我想要的那样工作:
fh<-gzfile("junk.gz")
headers<-readLines(fh,3)
nx<-as.numeric(readLines,1)
x<-scan(fh,nx)
ny<-as.numeric(readLines,1)
y<-scan(fh,ny)
这样做有点效果,但我还需要计算跳过的值:
...
x<-scan(fh,skip=3,nx)
...
啊...我发现使用gzfile()打开文件不能对数据进行搜索操作,所以所有的scan()函数都会重新开始并从文件开头开始。如果我解压缩文件并对未压缩的数据进行操作,我可以使用readLines(fh,n)和scan(fh,n=n)逐步读取各个部分。
readVector<-function(fh,skip=0,what=double()){
if (skip !=0 ){junk<-readLines(fh,skip)}
n<-scan(fh,1)
scan(fh,what=what,n=n)
}
fh<-file("junk")
headers<-readLines(fh,3)
x<-readVector(fh)
y<-readVector(fh)
xl<-readVector(fh)
yl<-readVector(fh)
... # still need to process a parenthesized complex array, but that is a different problem.
grep
来协助处理。 - A5C1D2H2I1M1N2O1R2T1