2

我正在尝试在 R 中使用“doSNOW”进行并行处理。在 foreach 循环命令期间,它显示错误:找不到对象 j。请在这方面帮助我。该代码适用于正常的 for 循环。但需要像永恒一样才能完成。请在这方面帮助我。我将永远感激不尽。

library(‘ncdf4′)
stryear = 2041
strmon = 1
strday = 1
library(‘foreach’)
library(‘doSNOW’)
cl <- makeCluster(6)
registerDoSNOW(cl)

filename = 'tasmax_WAS-44i_MPI-ESM-LR_rcp45_r1_CSIRO-CCAM-1391M_day_2041_2070.nc'
nc <- nc_open(filename)
tasmax <- ncvar_get(nc)
lon <- ncvar_get(nc, 'lon')
lat <- ncvar_get(nc, 'lat')
tasmax.len <- length(tasmax[1,1,])
x<-lon
lon.sel 87 & x<94)
y <- lat
lat.sel 20 & y<26)
lon.val <- lon[lon.sel]
lat.val <- lat[lat.sel]
#lon.length <- lon.val
#lat.length <- lat.val

strdate <- as.Date(paste(stryear,'-',strmon,'-', strday,sep=""))
finalVar <- data.frame( strday, strmon,stryear,matrix(NA,nrow=1, ncol=168))
colnames(finalVar)[1] <- "day"
colnames(finalVar)[2] <- "month"
colnames(finalVar)[3] <- "year"

locationData <- data.frame(matrix(NA,nrow=168, ncol=3))
colnames(locationData)[1]<-'y'
colnames(locationData)[2]<-'x'
colnames(locationData)[3]<-'Value'
n=1
rowno = 1

foreach(i = 1:12)
{ foreach(j = 1:14)
{ finalVar[rowno,n+3] <- tasmax[lon.sel[j],lat.sel[i],rowno]
colnames(finalVar)[n+3] <- paste(round(lat.val[i],2), ' & ', round(lon.val[j],2), sep="")
locationData[n,1] <- round(lat.val[i],2)
locationData[n,2] <- round(lon.val[j],2)
locationData[n,3] <- n
n= n+1
}
}

Error: object 'j' not found
4

0 回答 0