Sometimes it is important to know the number/proportion of cells in a raster that meet a certain threshold. This can easily be done as follows:
set.seed(123)
r <- raster(nrow = 10, ncol = 10) # Creates the raster.
r[] <- rnorm(1:ncell(r)) # Fills the raster with random values around 0.
plot(r) # Visualizes the raster.
ncell(r) # Counts the number of cells in the raster.
## [1] 100
sum(r[] > 0.3) # Returns sum of number of cells with pixel values above 0.3.
## [1] 41
sum(r[] > 0.3) / ncell(r) # Divides sum of cells above 0.3 by total number of cells.
## [1] 0.41
print(paste0(sum(r[] > 0.3) / ncell(r) * 100, '%'))
## [1] "41%"
Let’s assume our raster showed suitability scores of our study area for a specific species. So, we are aware that 41 % of our predicted area is suitable for the species. This value is normally useful when working around significance of the built model before interpreting results.