A fast data.table-based alternative to
raster::subs().
Arguments
- x
Categorical
RasterLayerwith integer values giving field class.- dict
A
data.frameordata.tablewith one (or possibly more) columns corresponding to the values of cells inxand one (or possibly more) columns giving the value to which each value inxshould be mapped.- by
Vector of one or possibly more integers or character strings giving the indices or names of the column in
dictcontaining the categorical values inx.- which
Vector of one or possibly more integers or character strings giving the indices or names of the column in
dictwith the numerical values to which each value inbyshould be mapped.- subsWithNA
Logical. If
TRUEvalues that are not matched become NA. IfFALSE, they retain their original value (which could also beNA). This latter option is handy when you want to replace only one or a few values. It cannot be used whenxhas multiple layers- filename
Character string giving (optional) file name to which the resultant raster should be written.
- ...
Additional arguments as for
raster::writeRaster(), on which this function relies.
Examples
r <- raster(ncol = 10, nrow = 10)
r[] <- round(runif(ncell(r)) * 10)
df <- data.frame(id = 2:8, v = c(10, 10, 11, 11, 12:14))
x <- subsDT(r, df)
x2 <- subsDT(r, df, subsWithNA = FALSE)
df$v2 <- df$v * 10
x3 <- subsDT(r, df, which = 2:3)
s <- stack(r, r*3)
names(s) <- c("first", "second")
x4 <- subsDT(s, df)
x5 <- subsDT(s, df, which = 2:3)