A fast data.table
-based alternative to
raster::subs()
.
Arguments
- x
Categorical
RasterLayer
with integer values giving field class.- dict
A
data.frame
ordata.table
with one (or possibly more) columns corresponding to the values of cells inx
and one (or possibly more) columns giving the value to which each value inx
should be mapped.- by
Vector of one or possibly more integers or character strings giving the indices or names of the column in
dict
containing the categorical values inx
.- which
Vector of one or possibly more integers or character strings giving the indices or names of the column in
dict
with the numerical values to which each value inby
should be mapped.- subsWithNA
Logical. If
TRUE
values 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 whenx
has 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)