I essentially have the same issue described here, but this question has not been answered Can I resample a raster in R using "max" as function?.

I have a elevation raster with a resolution of 100m. I would like to resample to create a new dataset that has the same grid as another raster I'm using, lets say landcover, which has a resolution of 4500m. I would like the value in the new dataset to be the standard deviation of elevation within the new grid cell.

It seems to me like both the aggregate and resample functions each do one of the things I need to do, but not the other. Aggregate would allow me to specify the stdev function, but it doesn't seem like I can get the grid to match another raster. Resample would allow me to match the grid of another raster, but I can't use stdev as the resampling function.

How could I approach this problem?

  • 1
    Resampling doesn't in general result in an exact overlap of source-destination grid cells, so what do you want to do about all the bits of cells that overlap? You could convert to polygons and do intersections and weighted calculations, but I don't really understand what you get out of doing a "resample with summary function" in the general case.
    – Spacedman
    Feb 8, 2022 at 20:41
  • 2
    Maybe exactextractr::exact_resample(elev, landcov, 'stdev') ?
    – dbaston
    Feb 10, 2022 at 0:22

1 Answer 1


You can first resample to a high resolution raster that is aligned with the low resolution raster and then use aggregate

Example data:

f <- system.file("ex/elev.tif", package="terra")
low <- rast(f)
high <- aggregate(rast(low), 5) |> extend(c(2,2)) |> shift(.04, .04)


dr <- res(high) / res(low)
a <- disagg(high, dr)
# using "near" to avoid smooting
a <- resample(r, a, "near")
b <- aggregate(a, dr, "sd", na.rm=TRUE)

But this should be more precise (thanks to @dbaston's comment):

e <- exactextractr::exact_resample(low, high, 'stdev')

The results are similar, see

plot(b, e); abline(0,1)
  • Thanks. I used the exact_resample method, this was exactly what I was looking for. Mar 18, 2022 at 15:50
  • @RobertHijmans I think aggregate(rast(r), 5) should be aggregate(rast(f), 5). Can you please check it?
    – UseR10085
    Jan 12, 2023 at 5:53
  • Thanks, the intent was rast(low) (fixed now) Jan 12, 2023 at 17:18
  • Not entirely sure if I got this right, but with just aggregate() it seems to be possible to process/write files larger than memory. With exactextractr::exact_resample() this is not possible, right? aggregate() uses some position/pixel (lower left?) as starting point to aggregate, is it somehow possible to set this manually to make it possible to align with the low resolution raster but process larger than memory rasters? Aug 18, 2023 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.