I'm trying to extract cell values and coordinates from raster using a polygon in R. I have multiple raster layers with different types of values, all within the same extent. I have multiple polygons within these rasters data set areas.
I want to use the polygons to extract the raster cell values and coordinates within each cell.
I'm using this code for example:
library("raster")
library("rgdal")
library("shapefiles")
library("rgeos")
library("sp")
values_cps <- extract(CPs,cp_layer,cellnumbers=T)
cp_values <- as.data.frame(values_cps)
write.csv(cp_values,file="RCP95_cps.csv")
where: CPs
is a raster layer, cp_layer
is a polygon.
When I write the csv it gives me a column of cell ID numbers and a column of the values of each cell. I want to get the coordinates to come with it.
I've tried using coordinates()
which I'm successfully using for the raster file, but not the extracted file.
Any ideas?