I want to modify the following code so that for all values beyond the defined maximum distance I can use the average values of points which fall in the same soil order category. Any recommendations?
# load packages and data
library(gstat)
library(raster)
data(meuse)
data(meuse.grid)
##################
meuse <- meuse[110:155,]
meuse <- na.omit(meuse)
coordinates(meuse) <- ~x+y
coordinates(meuse.grid) <- ~x+y
gridded(meuse.grid) <- TRUE
spplot(meuse.grid[4], main="soil class")
# inverse distance prediction for maximum distance of 2km.
ca.idw = idw(cadmium ~ 1, meuse, meuse.grid, omax =5, maxdist = 2000)