1

I want to calculate the average rain by region using the over function in R but I am getting the error "unable to find an inherited method for function over for signature RasterLayer, SpatialPolygonsDataFrame". This is my code:

rain <- readOGR(dsn="/Users___.shp", layer="rain")
class(rain)
#SpatialPolygonsDataFrame

region <- readOGR(dsn="/Users___.shp", layer="region")
class(region)
#SpatialPolygonsDataFrame

s <- raster(ncol=90, nrow=90)
extent(s) <- extent(rain)
rain_raster <- rasterize(rain, s)
class(rain_raster)
#RasterLayer

test1 <- over(rain_raster, region, fn="mean")
#error: unable to find an inherited method for function ‘over’ for signature ‘"RasterLayer", "SpatialPolygonsDataFrame"

How can I calculate this average without getting this error?

1 Answer 1

0

over doesn't do raster-polygon computations. You need something like extract from the raster package:

This will return the mean of all raster cells in each part of region:

 test1 <- raster::extract(rain_raster, region, fun=mean)
2
  • I tried using extract but I am getting an error.
    – Amy
    Commented Mar 24, 2021 at 10:18
  • If extract doesn't work, please edit your question and show us the problem there.
    – Spacedman
    Commented Mar 24, 2021 at 11:06

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.