I am trying to simultaneously discretize 5 stacked rasters in R into quartile values. I've written the following loop to do so, but it doesn't seem to be working correctly. In the code, "stack.disc" is the stack and "quartiles" is a 5 column data frame with the 5 rasters as columns and their quartile values listed in the rows.
for(i in 1:ncol(quartiles)) {
for(j in 1:length(stack.disc@layers[[i]])) {
if(isTRUE(stack.disc@layers[[i]]@data@values[j] >= quartiles[1,i] &
stack.disc@layers[[i]]@data@values[j] < quartiles[2,i])) {
stack.disc@layers[[i]]@data@values[j] = 1
}
if(isTRUE(stack.disc@layers[[i]]@data@values[j] >= quartiles[2,i] &
stack.disc@layers[[i]]@data@values[j] < quartiles[3,i])) {
stack.disc@layers[[i]]@data@values[j] = 2
}
if(isTRUE(stack.disc@layers[[i]]@data@values[j] >= quartiles[3,i] &
stack.disc@layers[[i]]@data@values[j] < quartiles[4,i])) {
stack.disc@layers[[i]]@data@values[j] = 3
}
if(isTRUE(stack.disc@layers[[i]]@data@values[j] >= quartiles[4,i] &
stack.disc@layers[[i]]@data@values[j] <= quartiles[5,i])) {
stack.disc@layers[[i]]@data@values[j] = 4
}
}
}
The code runs but only on the first 3 rasters in the stack. Any ideas why it isn't working for the last 2?
Thank you!