I want to overlap a species distribution range on the world map which has values, and extract the mean value within the species range.
I have 1500+ species distribution range in a gdb file so I try to use R to do all steps.
library(rgdal)
library (raster)
library(maptools)
#import species distribution range
data(species distribution range map)
test.sp <- readOGR(species distribution range map)
plot(test.sp, main="test.sp")
#import value map
r <- raster(the world map which has values)
#crop two maps and take the overlap range
r2 <- crop(r, extent(test.sp))
r3 <- mask(r2, test.sp)
plot(r3)
plot(test.sp, add=TRUE, lwd=2, main="crop_pic")
#catch the mean value of the overlap range-1
me <- mean(r3)
me
#failed: no mean value.
#catch the mean value of the overlap range-2
mea <- calc(r3, fun = mean)
mea
#failed: no mean value
#unable to find an inherited method for function ‘calc’ for signature ‘"RasterLayer", "RasterLayer"
##catch the mean value of the overlap range-3
mean <- extract(r3)
mean
Now I have a problem about mean value: I use three method to extract the mean value, but they all exclude mean value. Here is the result:
class : RasterLayer dimensions : 403, 167, 67301 (nrow, ncol, ncell) resolution : 0.01012254, 0.008160005 (x, y) extent : 127.2908, 128.9813, -0.9966684, 2.291813 (xmin, xmax, ymin, ymax) crs : +proj=longlat +datum=WGS84 +no_defs source : memory names : layer values : 3.954309e-05, 41 (min, max)
How to fix code to solve this?