Search code examples
rraster

R : How to find the location of the max value in a raster?


How can I find a location of max. value in a raster using R? I know the max. value of the raster, but I need to find its location.


Solution

  • Let's try with a toy raster as follows:

    library(raster)
    r = raster(nrow=10, ncol=10)
    r[] = runif(100,0,10)
    

    Then the position (index) of the maximum is found using

    idx = which.max(r)
    

    And from thhe index position to the coordinates of the cell

    pos = xyFromCell(r,idx)
    

    Let me know if it works