r-Error using foreach loop for parallel processing

136 Views Asked by At

I am trying to use 'doSNOW' for parallel processing in R. During the foreach loop command, it is showing error: object j is not found. Please help me in this regard. The code works fine with the normal for loop. But is takes like an eternity to finish. Please help me in this regard. I will be ever grateful.

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

There are 0 best solutions below