given is a raster as well as a SpatialPolygonsDataframe. In order to retrieve the highest value of the raster within the area of a polygon, raster::extract can be used. It works fine.
How to get additionally the coordinates of the extracted highest value of the raster within the area of a polygon?
# create raster
r <- raster(ncol=36, nrow=18)
r[] <- runif(ncell(r))
# create SpatialPolygons from GridTopology
grd <- GridTopology(c(-150, -50), c(40, 40), c(8, 3))
Spol <- as(grd, "SpatialPolygons")
# create SpatialPolygonsDataFrame
centroids <- coordinates(Spol)
x <- centroids[,1]
y <- centroids[,2]
SPDF <- SpatialPolygonsDataFrame(Spol, data=data.frame(x=x, y=y, row.names=row.names(Spol)))
# extract max value of raster for each SpatialPolygon
ext <- raster::extract(r, SPDF, fun=max)
*example code is taken from R-documentation