I have Cropscape Data Layer raster with different vegetation types. I want to only keep pixels with values 43 and/or 54. I'm doing something wrong with the reclassify()
function in R's raster
package.
Here is a demo raster and the reclassification table I use for reclassifying:
library(raster)
r1 <- raster(ncol=10,nrow=10)
values(r1) <- c(rep(10,10), rep(43,10), rep(44,20), rep(54,40), rep(70,20))
rcl <- matrix(data=c(-1,42,NA,43,43,1,44,53,NA,54,54,2,55,255,NA), ncol=3, byrow=T)
#also tried
#rcl <- matrix(data=c(1,42,0,43,43,1,44,53,0,54,54,2,55,255,0), ncol=3, byrow=T)
I tried
r2 = reclassify(r1, rcl, right=NA)
And I still get a bunch of zero values. In this example case, 50 zeros. I want the raster to hold only the pixels of interest, those valued 1 and 2. In this case, the raster would return the 10 pixels valued at 1 and the 40 pixels valued at 2.
I did something similar to this sometime last year and created the rasters as I wanted. I have notes, but not the complete code and it looks like I used a different reclassification table with 2 columns instead of 3 columns.
rcl <- matrix(data=c(43,1,54,2), ncol=2, byrow=T)
and
r2 = reclassify(r1, rcl, otherNA=TRUE)
But that doesn't do now what I want.