Learn R Programming

raster (version 2.0-05)

substitute: Substitute values in a Raster* object

Description

Substitute (replace) values in a Raster* object with values in a data.frame. The data.frame should have a column to identify the key (ID) to match with the cell values of the Raster* object, and one or more columns with replacement values. By default these are the first and second column but you can specify other columns with arguments by and which.

Usage

## S3 method for class 'Raster,data.frame':
subs(x, y, by=1, which=2, subsWithNA=TRUE, filename='', ...)

Arguments

x
Raster* object
y
data.frame
by
The column number or name that is the key (ID) to match a the data.frame row to a value of the Raster object. Default is 1
which
The column number or name that has the new (replacement) values. Default is 2
subsWithNA
Logical. If TRUE values that are not matched become NA. If FALSE, they retain their original value (which could also be NA. This latter option is handy when you want to replace only one or a few values
filename
Character. output filename
...
Additional arguments as for writeRaster

Value

  • A new Raster object, and in some cases, the side effect of a new file on disk.

Details

You could obtain the same result with reclass, but subs is more efficient for simple replacement. Use reclass if you want to replace ranges of values with new values. You can also replace values using a fitted model. E.g. fit a model to glm or loess and then call predict

See Also

reclass, cut

Examples

Run this code
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 <- subs(r, df)
x2 <- subs(r, df, subsWithNA=FALSE)

df$v2 <- df$v * 10
x3 <- subs(r, df, which=2:3)

s <- stack(r, r*3)
names(s) <- c('first', 'second')
x4 <- subs(s, df)
x5 <- subs(s, df, which=2:3)

Run the code above in your browser using DataLab