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?

asked Mar 24, 2021 at 2:57

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)
answered Mar 24, 2021 at 8:15
2
  • I tried using extract but I am getting an error. Commented Mar 24, 2021 at 10:18
  • If extract doesn't work, please edit your question and show us the problem there. Commented Mar 24, 2021 at 11:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.