user1667816
user1667816

Reputation: 31

Reclassify values of raster but max value of output raster seems incorrect

I have already searched for some other questions and found a couple that refer to the reclassify function but none of them specifically answer the problem that i seem to have. I have coded the values of a raster containing slope information in degrees incline. Data is extracted within each of my 15 polygons, constructed by GPS points. I would like the categories to be represented by the intervals: (0, 5, 1, 10, 15, 2, 20, 50, 3).

This leaves 3 classes but whenever I run the script below the min / max values of the raster are displayed as the normal spread of the data NOT 0-3 for the newly coded classes.

All50HRs <- shapefile("L:\GPSdatabackup\KERNELS\NewHRs50")
plot(All50HRs)
All50HRs
library(raster)
update.packages("raster")
recl <- matrix(c(0, 5, 1, 10, 15, 2, 20, Inf, 3), ncol=3, byrow=TRUE)
rcl <- matrix(ncol=3, byrow=TRUE)
rc <- reclassify(Slope, c(0, 5, 1, 10, 15, 2, 20, 50, 3))

Whenever I now call the image(recl) command to see the properties of the output raster it has min = 0 and max = 19.9 where it should be 3!

Does anyone know why this is as I am relatively new with R?

Upvotes: 2

Views: 3860

Answers (1)

jbaums
jbaums

Reputation: 27388

You have forgotten to reclassify the values between 5 and 10, and between 15 and 20, so those values remain as they were to begin with.

To correct the problem, fix your reclassification vector/matrix as in the following example:

r <- raster(matrix(runif(100, 0, 50), ncol=10))
plot(r)

enter image description here

m <- matrix(c(0, 5, 1,
            5, 10, 2,
            10, 15, 3,
            15, 20, 4,
            20, Inf, 5), ncol=3, byrow=TRUE)
r2 <- reclassify(r, m)
r2

# class       : RasterLayer 
# dimensions  : 10, 10, 100  (nrow, ncol, ncell)
# resolution  : 0.1, 0.1  (x, y)
# extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
# coord. ref. : NA 
# data source : in memory
# names       : layer 
# values      : 1, 5  (min, max)

plot(r2)

enter image description here

See ?reclassify for more details.

Upvotes: 3

Related Questions