Search code examples
rrastercliprastervis

Clipping of raster as a shapefile in r


I want to clip a raster using shapefile in r as a shape of shapefile. I used crop function but it clipping as extents not as a shape. Please help me


Solution

  • Crop, as stated in the manual pages, crops to the extent of the object, for your purposes you may use raster::mask(), with the same syntaxis of crop mask function "Creates a new Raster* object that has the same values as x, except for the cells that are NA (or other maskvalue) in a ’mask’." You may pass inverse=T so you get those values out of the spatialPolygon.