How to create a weighted mean SpatRaster out of multiple SpatRasters in R terra

118 Views Asked by At

I would like to create a raster, which represents the weighted mean of a number of SpatRasters per cell, i.e., I want a new raster where for every cell at position [x,y], this cell will have the weighted mean of the input SpatRasters' cells at positions [x,y].

The weights should be applied per whole raster, e.g., if raster_1 has weight 4, raster_2 2 and raster_3 1, then for every cell of the resulting raster: weighted_mean_raster[x,y] = (raster_1[x,y] * 4 + raster_1[x,y] * 2 + raster_1[x,y] * 1) / (4 + 2 + 1)

I am aware of the app function, and I have used it before to create a non-weighted mean raster:

# new raster with the mean of each cell value of the 3 other rasters
mean_raster = app(raster_1, raster_2, raster_3, fun=mean)

However, I cannot get this approach to work when fun=terra::weighted.mean whichever inputs I try to put into "weighted.mean"

Thank you very much in advance for all your help!

0

There are 0 best solutions below