1

Following one of my previous questions about looping through a rasterstack (here), I now face a different issue. My initial rasterstack had dates as names (e.g. X2000_05_08.1) and I reclassified it in order to change specific values. After the reclassification though, I got a raster brick and the names changed from dates to layer.1, layer.2 and so on.

Can anyone help me to understand what I did wrong and how to fix this as I want to keep the dates?

Reclassify code:

m <- c(-1, 0,NA) 
rclmat <- matrix(m, ncol=3, byrow=TRUE) # matrix with the value to reclass
new_rc <- reclassify(new, rclmat,right=FALSE) # reclassify my stack ("new")
1

Using the example in the help for reclassify (because we can't run your code because we don't have your new object)...

If a raster has names:

> names(r)="Region-A"

and is reclassified....

> rc <- reclassify(r, rclmat)

the name is not preserved:

> names(rc)
[1] "layer"

This also applies if you have a stack:

> s = stack(r,r,r,r,r,r,r)

with names...

> names(s)=paste0("Region ",LETTERS[1:7])

and reclassify:

> sc <- reclassify(s, rclmat)
> names(sc)
[1] "layer.1" "layer.2" "layer.3" "layer.4" "layer.5" "layer.6" "layer.7"

the fix is to reset the names on the reclassified stack from the original stack:

> names(sc) = names(s)
> names(sc)
[1] "Region.A" "Region.B" "Region.C" "Region.D" "Region.E" "Region.F" "Region.G"
> 

If you do this a lot then write a wrapper (untested, but should be something like):

nreclassify = function(r, ...){
  rc = reclassify(r, ...)
  names(rc) = names(r)
  return(rc)
 }

you might also consider suggesting it as an enhancement to the raster package, but the author might have a good reason for not doing this by default.

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.