rterra

Weighted mean returns empty raster


I can't figure out why this returns an error yet these are the values I need to work with.

library(terra)
#> terra 1.7.78
r1 <- rast(ncols=10, nrows=10, nlyr=6, vals=0.00:0.56)
r2 <- rast(ncols=10, nrows=10, nlyr=6, vals=0.00000000:0.03338733)
r3 <- weighted.mean(r1, r2, na.rm=TRUE)

Solution

  • Since all your weights are zero, weighted means cannot be computed. With more sensible weights this appears to work:

    library(terra)
    set.seed(1)
    r1 <- rast(ncols=10, nrows=10, nlyr=6, vals=sample(10, 600, replace=TRUE))
    r2 <- rast(ncols=10, nrows=10, nlyr=6, vals=sample(10, 600, replace=TRUE))
    r3 <- weighted.mean(r1, r2, na.rm=TRUE)
    r3
    #class       : SpatRaster 
    #dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
    #resolution  : 36, 18  (x, y)
    #extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
    #coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
    #source(s)   : memory
    #name        :      sum 
    #min value   : 2.214286 
    #max value   : 8.481481