I am trying to extract the mean of a raster data to polygons with the following code:
library(rgdal) library(raster) library(maptools) agri_suitability=raster("suit") regions <- readShapeSpatial("Regions.shp") #SpatialPolygonsDataFrame plot(agri_suitability) plot(regions, add=T)
polygon_agri_suitability=extract(agri_suitability,regions,fun=mean,na.rm=T) regions@data = data.frame(regions@data, polygon_agri_suitability) write.csv(regions,"suitability_mean.csv")
After data.frame I get an error:
arguments imply differing number of rows:1549, 0
My "Regions.shp" file in fact includes 1549 rows. But I don't know what this error means and how I can solve it.