I have a raster with multiple categories for which I am only interested in one category (vegClass).
<code>veg <- terra::rast("~/SVTM_NSW_Extant_PCT_vC2_0_M2_0_106/SVTM_NSW_Extant_PCT_vC2_0_M2_0_5m.tif")
activeCat(veg) <- 4 #set category of interest to veg class
veg
class : SpatRaster
dimensions : 1207, 1463, 1 (nrow, ncol, nlyr)
resolution : 0.0002777778, 0.0002777778 (x, y)
extent : 142.2135, 142.6199, -31.33736, -31.00208 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat GDA94
source(s) : memory
varname : 1_Second_DEM_MNP_N
categories : PCTID, PCTName, vegForm, vegClass, form_PCT, labels
name : vegClass
min value : Not classified
max value : Riverine Sandhill Woodlands
</code>
<code>veg <- terra::rast("~/SVTM_NSW_Extant_PCT_vC2_0_M2_0_106/SVTM_NSW_Extant_PCT_vC2_0_M2_0_5m.tif")
activeCat(veg) <- 4 #set category of interest to veg class
veg
class : SpatRaster
dimensions : 1207, 1463, 1 (nrow, ncol, nlyr)
resolution : 0.0002777778, 0.0002777778 (x, y)
extent : 142.2135, 142.6199, -31.33736, -31.00208 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat GDA94
source(s) : memory
varname : 1_Second_DEM_MNP_N
categories : PCTID, PCTName, vegForm, vegClass, form_PCT, labels
name : vegClass
min value : Not classified
max value : Riverine Sandhill Woodlands
</code>
veg <- terra::rast("~/SVTM_NSW_Extant_PCT_vC2_0_M2_0_106/SVTM_NSW_Extant_PCT_vC2_0_M2_0_5m.tif")
activeCat(veg) <- 4 #set category of interest to veg class
veg
class : SpatRaster
dimensions : 1207, 1463, 1 (nrow, ncol, nlyr)
resolution : 0.0002777778, 0.0002777778 (x, y)
extent : 142.2135, 142.6199, -31.33736, -31.00208 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat GDA94
source(s) : memory
varname : 1_Second_DEM_MNP_N
categories : PCTID, PCTName, vegForm, vegClass, form_PCT, labels
name : vegClass
min value : Not classified
max value : Riverine Sandhill Woodlands
I want to keep only some values and want to set everything else to NA.
<code>values_to_keep <- c("Stony Desert Mulga Shrublands", "Gibber Chenopod Shrublands", "Inland Rocky Hill Woodlands")
</code>
<code>values_to_keep <- c("Stony Desert Mulga Shrublands", "Gibber Chenopod Shrublands", "Inland Rocky Hill Woodlands")
</code>
values_to_keep <- c("Stony Desert Mulga Shrublands", "Gibber Chenopod Shrublands", "Inland Rocky Hill Woodlands")
I’m stuck on how to do this. I’ve tried converting it to a dataframe and filtering but the raster doesn’t have xy columns so the coordinate data is lost when I try to convert it back. Any help is appreciated!
New contributor
user24984035 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.