[GRASS-stats] Reclassyify in R

Jarosław Jasiewicz jarekj at amu.edu.pl
Thu Nov 29 03:47:47 EST 2007


Dear all.


Working on my problem I stopped on another issue.
I try to reclassify SpatialGridDataFrame acording to vector file:
let say:
- slope imported from GRASS (sp_layer, numeric vector from 
SpGridDataFrame  slope$slope)
-density obiect made in R (iloraz_obiect, with density$x with ald values 
and density$y with new vlues)

And I have very primitive (step by step) solution:

reclass<-function(sp_layer,dens_iloraz_object) {

tmp_k<-0
layer_length<-length(sp_layer) #number of values in reclasified vector, 
about million
dens_length<-length(dens_iloraz_object$x) #number of class of reclassify

for (i in seq(1:layer_length)) {  #loop for million iteration
    if (is.na(sp_layer[i])) tmp_k[i]<-NA else { #test if there is NA value
        if (sp_layer[i]==dens_iloraz_object$x[dens_length]) #test if it 
is max and last value of recllsifying vector
            {tmp_k[i]<-dens_iloraz_object$y[dens_length] } else {
                for (j in seq(1:(dens_length-1))) { #reclasify according 
rules loop about 32 or 64 rules
                if (!is.na(tmp_k[i])) break #if done! break!
                    if (sp_layer[i]>=dens_iloraz_object$x[j] & 
sp_layer[i]<dens_iloraz_object$x[j+1]) tmp_k[i]<-dens_iloraz_object$y[j]
                }
        }
    }
}
return (tmp_k)

} #end funcion

What is the problem If works?      it is komputer killer.


I have solution which seems faster:

reclass<-function(sp_layer,dens_iloraz_object) {

tmp_k<-NA
dens_length<-length(dens_iloraz_object$x)

    if (sp_layer==dens_iloraz_object$x[dens_length]) 
{tmp_k<-dens_iloraz_object$y[dens_length] } else {
        for (j in seq(1:(dens_length-1))) {
            if (!is.na(tmp_k)) break
                if (sp_layer>=dens_iloraz_object$x[j] & 
sp_layer<dens_iloraz_object$x[j+1]) tmp_k<-dens_iloraz_object$y[j]
        }
    }
   
return (tmp_k)
}
 and next:
k=ifelse(is.na(sp_layer),NA,reclass(sp_layer,iloraz_dens))

but I cannot handle if (sp_layer==......) in function, which proceeds on 
vector and use only first element



I look for little faster solution
Maybe someone can help me?

Jarek


More information about the grass-stats mailing list