0

I have a very large raster stack of 6253 raster layers and I want to reclassify it. For the reclass, I have tried the following but with no success:

my_rules <- c(-0.500, -0.251, 1,  -0.251, -0.101, 2,  -0.101,0.99,3, 0.99,0.269,4, 0.269,0.439,5, 0.439,0.659,6, 0.659,1.300,7, 1.300,2,NA)
reclmat <- matrix(my_rules, ncol=3, byrow=TRUE)
my_stack<- reclassify(my_stack, reclmat,right=FALSE)

Error in .local(x, rcl, ...) : all(rcl[, 2] >= rcl[, 1]) is not TRUE

Is this the wrong way to approach this?

3

Your reclassification matrix isn't right - I suspect the 0.990 on rows 3 and 4 should be 0.099 - as it is the rows make up overlapping classes so the function throws an error:

> reclmat
       [,1]   [,2] [,3]
[1,] -0.500 -0.251    1
[2,] -0.251 -0.101    2
[3,] -0.101  0.990    3
[4,]  0.990  0.269    4
[5,]  0.269  0.439    5
[6,]  0.439  0.659    6
[7,]  0.659  1.300    7
[8,]  1.300  2.000   NA

Its nothing to do with the size of the stack - you should always test things first on small examples. I duplicated your error with:

> r = raster()
> r[] = runif(ncell(r),-0.5,2)
> reclassify(r,reclmat)
Error: all(rcl[, 2] >= rcl[, 1]) is not TRUE

Change the 0.990 to 0.099 and it works:

> my_rules <- c(-0.500, -0.251, 1,  -0.251, -0.101, 2,  -0.101,0.099,3, 0.099,0.269,4, 0.269,0.439,5, 0.439,0.659,6, 0.659,1.300,7, 1.300,2,NA)
> reclmat <- matrix(my_rules, ncol=3, byrow=TRUE)
> reclassify(r,reclmat)
class       : RasterLayer 
dimensions  : 180, 360, 64800  (nrow, ncol, ncell)
resolution  : 1, 1  (x, y)
  • Such a stupid mistake from me. Thank you for the notice @Spacedman! – foo Apr 2 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.