1

複数の for ループの組み合わせに問題があります。そのうちの2つを例に挙げて、組み合わせたいと思います。2 つでそれを行う方法を知っていれば、複数のループで行うこともできます。

誰かがこれをlapply関数として書く方法を知っていれば、それもいいでしょう。

require(ncdf4)

#### download files from this link to directory: (I just downloaded manually,two files are sufficient to answer the example)
#### ftp://rfdata:forceDATA@ftp.iiasa.ac.at/WFDEI/LWdown_daily_WFDEI/

setwd("C:/place_where_I_have_downloaded_my_files_from_link/")
temp = list.files(pattern="*.nc")                         #list imported netcdf files
list2env(
  lapply(setNames(temp, make.names(gsub("*.nc$", "", temp))), 
         nc_open), envir = .GlobalEnv) #import all parameters lists to global environment

#### first loop                               -                # select parameter out of netcdf files and combine into a List of 2
list_temp<-list()                                         #create empty list before loop
for (t in temp[1:2]){
  list_temp[t]<-list(data.frame(LWdown=ncvar_get(nc_open(t),"LWdown")[428,176,],xcoor=176,ycoor=428))
}
LW_bind<-do.call(rbind,list_temp)
rownames(LWdown_1to2)<-NULL

#### second loop                                                    # select parameter out of onenetcdf file per x-coordinate and combine into a List of 2
list_temp<-list()                                         #create empty list before loop
for (x in 176:177){
  list_temp[t]<-list(data.frame(LWdown=ncvar_get(nc_open(temp[1]),"LWdown")[428,x,],xcoor=x,ycoor=428))
}
LW_bind<-do.call(rbind,list_temp)
rownames(LWdown_1to2)<-NULL

結合しようとしたがうまくいかなかった方法:

#### combined loops
list_temp<-list()
for (t in temp[1:2]){for (x in 176:177){
  #ncin<-list()
  ncin<-nc_open(t)
  list_temp[x][t]<-list(data.frame(LWdown=ncvar_get(ncin,"LWdown")[428,x,],x=x,y=428))
}}
LWdown_1to2<-do.call(rbind,list_temp)
rownames(LWdown_1to2)<-NULL

私はすでに私の問題を解決しました。下記参照。しかし、上記のように 2 つの for ループをどのように解決できるのか、まだ興味があるので、質問は未回答のままにしておきます。

これが私の解決策です:

require(arrayhelpers);require(stringr);require(plyr);require(ncdf4)
# store all files from ftp://rfdata:forceDATA@ftp.iiasa.ac.at/WFDEI/ in the following folder:
setwd("C:/folder")
temp = list.files(pattern="*.nc")           #list all the file names
param<-gsub("_\\S+","",temp,perl=T)         #extract parameter from file name

xcoord=seq(176,180,by=1)                    #The X-coordinates you are interested in
ycoord=seq(428,433,by=1)                    #The Y-coordinates you are interested in

list_var<-list()                         # make an empty list
for (t in 1:length(temp)){
temp_year<-str_sub(temp[],-9,-6)                                                                                #take string number last place minus 9 till last place minus 6 to extract the year from file name
temp_month<-str_sub(temp[],-5,-4)                                                                               #take string number last place minus 9 till last place minus 6 to extract the month from file name
temp_netcdf<-nc_open(temp[t])
temp_day<-rep(seq(1:length(ncvar_get(temp_netcdf),"day"))),length(xcoord)*length(ycoord))                   # make a string of day numbers the same length as amount of values
    dim.order<-sapply(temp_netcdf[["var"]][[param[t]]][["dim"]],function(x) x$name)                            # gives the name of each level of the array
    start <- c(lon = 428, lat = 176, tstep = 1)                                                                     # indicates the starting value of each variable
    count <- c(lon = 6, lat = 5, tstep = length(ncvar_get(temp_netcdf,"day")))                                 # indicates how many values of each variable have to be present starting from start
    tempstore<-ncvar_get(temp_netcdf, param[t], start = start[dim.order], count = count[dim.order])            # array with parameter values

df_temp<-array2df (tempstore, levels = list(lon=ycoord, lat = xcoord, day = NA), label.x = "value")           # convert array to dataframe
Add_date<-sort(as.Date(paste(temp_year[t],"-",temp_month[t],"-",temp_day,sep=""),"%Y-%m-%d"),decreasing=FALSE)  # make vector with the dates
list_var[t]<-list(data.frame(Add_date,df_temp,parameter=param[t]))                                         #add dates to data frame and store in a list of all output files
  ### nc_close(temp_netcdf)                                                                                           #close nc file to prevent data loss and errors
}
All_NetCDF_var_in1df<-do.call(rbind,list_var)  
4

0 に答える 0