Max cell value from a stacked raster
The following example shows two ways to get at the max raster value in a stack. The first utilizes max()
which also gives you a host of other useful information. The second method uses maxValue()
, which gives just the max value of both the rasters in the stack
library(raster)
# Generate some georeferenced raster data
x = matrix(rnorm(400),20,20)
rast = raster(x)
extent(rast) = c(36,37,-3,-2)
projection(rast) = CRS("+proj=longlat +datum=WGS84")
y = matrix(rnorm(400),20,20)
rast2 = raster(y)
extent(rast2) = c(36,37,-3,-2)
projection(rast2) = CRS("+proj=longlat +datum=WGS84")
raster = stack(rast, rast2)
# Now run the statistics
max(raster) # Provides min, max and additional details # Example 1
maxValue(raster) # Gives both values # Example 2...
maxValue(raster)[[1]] # Gives first in stack max value
maxValue(raster)[[2]] # Gives second in stack max value
> maxValue(raster) # Gives both values
[1] 2.688376 2.971443
> maxValue(raster)[[1]] # Gives first in stack max value
[1] 2.688376
> maxValue(raster)[[2]] # Gives second in stack max value
[1] 2.971443
>
> max(raster) # Provides min, max and additional details
class : RasterLayer
dimensions : 20, 20, 400 (nrow, ncol, ncell)
resolution : 0.05, 0.05 (x, y)
extent : 36, 37, -3, -2 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : layer
values : -1.457908, 2.971443 (min, max)