Reputation: 617
I have a raster and I would like to reclassify its values to four values (NA, 1, 2 and 3).
I used the reclassify
command from the raster
package. In this command it is possible to indicate an array containing the intervals that must be reclassified. But it seems to me that reclassify
only accepts a 3x3 array, so I can't reclassify more than 3 ranges.
I tried to assign an array m
containing all my ranges to the rcl
within the reclassify
command, but it only considers the first 3 rows, so the other rows of the array are ignored by the command.
the m
array works like this:
the first column represents the lower value range, the second column represents the upper value range, and the third column represents the new value assigned to that cell. For example, in the line at line 1 0,0,NA
I want all values equal to zero to be converted to NA. On line 2 1,5,1
, I want all values between 1 and 5 to be converted to value 1. On 7th line 16,19,3
for example, values between 16 and 19 should be converted to value equal to 3, and so on.
I don't know what's wrong with this conversion. Would I accept a solution with a raster
package or another package?
a exemple here
#raster
r <- raster(matrix(runif(100, 0, 50), ncol=10))
plot(r)
#matrix with rules reclass
m<-matrix(c(0,0,NA,
1,5,1,
6,8,3,
9,9,2,
10,13,1,
14,15,2,
16,19,3,
20,20,2,
21,23,3,
24,24,3,
25,25,3,
26,32,3,
33,33,3,
34,38,3,
39,39,2,
40,40,3,
41,41,2,
42,Inf,3), ncol=3, byrow=TRUE)
#r2 with reclass
r2<-raster::reclassify(x=r, rcl=m)
Upvotes: 0
Views: 1211
Reputation: 47146
It works better if there are no gaps in the reclassification matrix
#matrix with rules reclass
m<-matrix(c(0,1,NA,
1,6,1,
6,8,3,
8,9,2,
9,13,1,
13,15,2,
15,19,3,
19,20,2,
20,23,3,
23,24,3,
24,25,3,
25,32,3,
32,33,3,
33,38,3,
38,39,2,
39,40,3,
40,41,2,
41,Inf,3), ncol=3, byrow=TRUE)
#r2 with reclass
r2<-raster::reclassify(x=r, rcl=m, include.lowest=TRUE)
This has to be done like that with decimal numbers. With integers, it could be allowed to specify it the way you want think about it, where the interval is open at both sides, and from
and to
can be the same value, but that is currently not the case.
Upvotes: 1