【发布时间】:2016-08-31 13:28:25
【问题描述】:
我有如下所示的代码,它是用 R 编写的。它打算做的是读取文件夹中的一些 ascii 文件,一旦读取它们,它会将其数据一个附加到另一个文件中,同时排除每个初始文件的最后三行。然后它打算将最终文件保存在 csv 中。 我面临的问题是我的文件夹中写入了“final.csv”,但文件是空的....有人会知道为什么吗? 在这里,您将找到两个 ascii 文件,我打算将其中的数据附加到另一个之下,同时不包括每个文件的标题。
curl -g -O http://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2/M2T1NXSLV.5.12.4/2015/05/MERRA2_400.tavg1_2d_slv_Nx.20150501.nc4.ascii?T10M[0:23][241:241][343:343],lat[241:241],time[0:23],lon[343:343] -O http://goldsmr4.gesdisc.eosdis.nasa.gov/opendap/MERRA2/M2T1NXSLV.5.12.4/2015/05/MERRA2_400.tavg1_2d_slv_Nx.20150502.nc4.ascii?T10M[0:23][241:241][343:343],lat[241:241],time[0:23],lon[343:343]
我正在使用 OS X El Capitan v 10.11.4。以及 RStudio v 0.99.451 和 R 3.2.4。
setwd("/path to folder where the ascii files are located")
table<-list.files("/path to folder where the ascii files are located", full.names = TRUE)
table
# To read all the .ascii inside the folder
f<-function(x){
k<-1
y<-data.frame(x[k])
while(k<=length(x)){
k<-k+1
y<-data.frame(y,x[k])
}
return(y)
}
info_data<-f(table)
# To read the data of each ascii and exlude the values of the three lines of each ascii
h<-function(z){
s<-c()
t<-1
while(t<=length(z)-3){
s<-c(s,z[t])
s<-s+1
}
return(s)
}
# Append the data of each ascii, one below the other, in a single file and save it as csv
g<-function(y){
z<-c()
j<-1
r<-length(y[1,])
while(j<=r){
z<-append(z,h(y[,j]))
j<-j+1}
return (z)
}
final<-g(info_data)
write.csv(final, "final.csv", sep = ",")
【问题讨论】:
-
您在文件中的阅读情况如何?我在您的代码中没有看到任何读取功能(可能是
read.csv?)。