1

我在组合多个 for 循环时遇到问题。我将举两个例子,我想结合起来。如果我知道如何用两个来做,我也可以用多个循环来做。

如果有人知道如何将其编写为 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

我已经解决了我的问题。见下文。但我仍然很好奇如何解决上述两个 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