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

Robert Hijmans r.hijmans at gmail.com
Fri Jul 15 03:16:07 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. 

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

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

x  <- calc(data_stack, fun)


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

More information about the R-sig-Geo mailing list