SGDF loop processing time
On Wed, 25 Jun 2008, Ilona Naujokaitis-Lewis wrote:
Dear List-serve, I have written a loop that creates a Spatial Grid Dataframe from 2 other SGDFs. I am interested in finding out if there is a way to decrease the processing time of the entire loop. I am aware that loops in R are not the most efficient way to go, but based on my knowledge, I am not sure how to get around this. To avoid problems with memory allocation I have added gc() command throughout the loop. The system.time() results for the loop (see code below) with a small (16x12) grid is: user system elapsed 17.08 0.02 17.22 The problem is when I work with larger grids (i.e. greater than dimensions: 900x1200). The processing time is in the order of 24 hours. I have checked threads for help on both decreasing the memory size, and writing efficient code. Any suggestions on how I might decrease the processing time, as this is the biggest concern right now (both using and not using R, but the former preferred), would be greatly appreciated. Here is the code with a brief description of the objects. Objects: prePatchmap: SGDF, binary (0,1) mean_HS: mean habitat suitability value of original habitat suitability map sd_HS: standard deviation of original habitat suitability map new_HSthreshold: new habitat suitability threshold HSthreshold: original habitat suitability threshold HSmap: SGDF of original habitat suitability values
Could you provide an online example - save() the objects you need here and post the RData file? This does look vectorisable, at least in two chunks. Roger
for (i in 1:length(prePatchmap$patch))
{
if (prePatchmap$patch[i]==1)
{
prePatchmap$NPHS[i]<-round(rnorm(1, mean_HS, sd_HS),digits=3)
gc()
{
if (prePatchmap$NPHS[i]<new_HSthreshold)
{
prePatchmap$NPHS[i]<-new_HSthreshold
gc()
}
}
} else
{
if (HSmap$HSvalue[i]<HSthreshold)
{
prePatchmap$NPHS[i]<-HSmap$HSvalue[i]
}
gc()
}
}
Cheers,
Ilona Naujokaitis-Lewis
Ilona Naujokaitis-Lewis
Centre for Applied Conservation Research
Forest Sciences Department
University of British Columbia
3041-2424 Main Mall
Vancouver, BC V6T 1Z4
phone: 604 822.4382
email: ilonan at interchange.ubc.ca
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no