Skip to main content
2 of 2
deleted 37 characters in body; edited title
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Replacing raster values with closest value from list using R?

The task: I'm creating a function to assess flood damage in R. The inputs are:

  • Dataframe with water depth values and corresponding damage values in percentage.
  • Raster representing water depth

The raster values must be approximated to the closest value in the first column of the df and then create a new raster with damage values using corresponding values from the second column).

library(raster)
damage <- cbind(c(0.0,0.2,0.4,0.6,0.8,1.0),c(0, 5, 10, 40, 70, 100))
r.wd <- raster(myraster.tif)

I've tried using which.min(abs(damage[,1]-r.wd)) but doesn't work.