I recently had an email from a PhD student in Austria who had a raster showing the distribution of Douglas Fir in Europe and wanted to know what proportion of each European country was covered in this species. They had a raster with presence (1) and absence (0) of Douglas-fir in Europe and wanted to calculate the number of cells with 1 and 0 within each country of the Europe. I’ve put together a dummy example below which shows how to R script to extract the number of raster cells in each country that meet a certain condition.
Essentially the script works through the following steps:
- Loads the relevant shapefile and raster datasets.
- Identifies all of the countries within the shapefile.
- Within a loop, masks the presence-absence raster by each country and counts the number of cells that meet the required condition.