Reclassify

library(terra)

fire <- rast("data/AnalysisReady/burn_severity_ba4.tif")
dem <- rast("data/AnalysisReady/elevation.tif")

## Reclass values 0 and 9 to NA
# Create a reclassification matrix
m <- c(0, NA,
       9, NA)
rclmat <- matrix(m, ncol = 2, byrow = T)
# Reclassify 0 and 9 to NA
fire_rc <- classify(fire, rclmat)
# Reattach the color table 
coltab(fire_rc) <- coltab(fire)
plot(fire_rc)

## Reclassify so unburned and low severity = 1 and moderate and high severity = 2
m <- c(0, NA,
       1, 1,
       2, 1,
       3, 2,
       4, 2,
       9, NA)
rclmat <- matrix(m, ncol = 2, byrow = T)
fire_rc2 <- classify(fire, rclmat)

# Reclassify DEM into bins
m <- c(0, 1000, 1,
       1000, 2000, 2, 
       2000, 3000, 3, 
       3000, 4000, 4)
rclmat <- matrix(m, ncol = 3, byrow = T)
dem_rc <- classify(dem, rclmat, include.lowest = T)

dem_rc2 <- classify(dem, c(0, 1000, 2000, 3000, 4000), include.lowest = T, 
                    brackets = T)