[GRASS-stats] Reclassyify in R

2 messages Options
Embed this post
Permalink
Jarosław Jasiewicz

[GRASS-stats] Reclassyify in R

Reply Threaded More More options
Print post
Permalink
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
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand

Re: [GRASS-stats] Reclassyify in R

Reply Threaded More More options
Print post
Permalink
On Thu, 29 Nov 2007, Jaros?aw Jasiewicz wrote:

> 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

Try cut():

library(spgrass6)
slope <- readRAST6("slope@PERMANENT")
names(slope)
summary(slope)
library(classInt)
t1 <- classIntervals(slope$slope.PERMANENT, n=32, "pretty")
t2 <- print(t1)
slope$slcut <- cut(slope$slope.PERMANENT, breaks=t1$brks,
   labels=names(t2), include.lowest=TRUE)
table(slope$slcut)
t2

where you would construct breaks= and labels= from your x and y (here I
just used classIntervals() as an easy way to creat some breaks). An
alternative to cut() is findInterval(), and assign the labels
subsequently.

Roger

> Maybe someone can help me?
>
> Jarek
> _______________________________________________
> grass-stats mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/grass-stats
>

--
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: [hidden email]
_______________________________________________
grass-stats mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/grass-stats
Roger Bivand
Economic Geography Section
Department of Economics
Norwegian School of Economics and Business Administration
Helleveien 30
N-5045 Bergen, Norway