[R-sig-Geo] classify a raster by unique value

Robert Hijmans r.hijmans at gmail.com
Fri Jul 15 16:22:25 CEST 2011


>> I've a raster stack with 3 layers. 
>> 
>> I need to classify it assigning one class to each unique combination of
>> values. 

> Perhaps the below works. It may be very slow if unq has many rows. 

That was not quite right, I think it works when you do:

unq <- unique(as.matrix(data_stack)) 

fun  <- function(x, ...) { 
        r <- rep(NA, nrow(x)) 
        for (i in 1:nrow(unq)) { 
                r[ which(apply(t(x) == unq[i,], 2, sum) == ncol(unq)) ] <- i 
        } 
        r 
} 

x  <- calc(data_stack, fun) 



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/classify-a-raster-by-unique-value-tp6575119p6587070.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list