r-Error使用foreach循环进行并行处理

时间:2015-05-20 17:50:32

标签: r foreach rparallel

我正在尝试使用'doSNOW'在R中进行并行处理。在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

0 个答案:

没有答案