[R-sig-Geo] SGDF loop processing time

Ilona Naujokaitis-Lewis ilonan at interchange.ubc.ca
Wed Jun 25 18:56:34 CEST 2008


   
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
  
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




More information about the R-sig-Geo mailing list