I would like to interpolate the missing values (set to 0) within my SpatRaster object.
I created a SpatRaster object and assigned values to specific cells depending on cell coordinates:
raster_layer <- rast(ncol=22, nrow=11, xmin=-3819.721, xmax=1132.362,
ymin=-698.7546, ymax=2013.745)
for (i in 1:nrow(Test)) { #Test is the sample dataset I am pulling the coordinates
and the cell values from.
col_num <- Test$grid_column[i]
row_num <- Test$grid_row[i]
cell_value <- Test$pm1_mgm3[i]
raster_layer[row_num, col_num] <- cell_value
}
Plotting the raster layer gets me this result and I want to interpolate all missing values:
I initially used a solution from another similar question from this question:
r_interp <- raster_layer %>%
as.matrix() %>%
apply(MARGIN = 2, \(x) approx(x)[["y"]]) %>%
raster()
plot(r_interp)
However, I keep getting errors like "[rast,list] none of the elements of x are a SpatRaster" or "Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'rast': argument "FUN" is missing, with no default".
Reading the Terra documentation doesn't seem to help clarify how to write a simple interpolation function for a SpatRaster object.