I want to clip a raster in R based on a polygon. The analog function in arcgis that does the same is Data Management Tools -> Raster -> Raster Processing -> Clip.
What I have until now is:
library(raster) set.seed(123) r <- raster() res(r) <- c( 0.016666, 0.016666 ) extent <- shapefile("colombia_2000.shp") r <- crop(r, extent, snap='near') r <- setValues(r, sample(x=c(0,1),size=ncell(r), replace = T)) undata_mask <- mask(r, extent)
You can find the "extent" file in the following link:
The polygon (extent) I am using represents the area between 1000 an 2000 meters above see level in the Andean Mountain range in Colombia, and as you can see there are some internal patches that are not covered with the polygon, which belongs to the mountain peaks that exceed this altitud range.
When I perform the crop and mask functions in R, those internal patches remain untouched as if they belonged to the polygon. When I perform the calculation in ArcGis, the internal patches are indeed clipped, which is what I want to achieve in R.
How Can I get rid of this internal patches?
Here are some images of What I just said.
The first one is with R and the second one is with ArcGIS