Search code examples
rgisspatialrasterr-raster

Editing all raster cell values above a certain value in R


I am using a bathymetric map of the Arctic Ocean containing 11617*11617 cells, each with a value for height relative to sea level (from -5573 to 5921 m). I wish to edit all pixels with values greater than 0 m to have a value of negative 10 m, and then save this raster.

bath=raster ('C:/Users/ls15g11/Desktop/IBCAO_V3_500m_RR_editinR.grd')
bath

class       : RasterLayer 
dimensions  : 11617, 11617, 134954689  (nrow, ncol, ncell)
resolution  : 500, 500  (x, y)
extent      : -2904250, 2904250, -2904250, 2904250  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : C:\Users\ls15g11\Desktop\IBCAO_V3_500m_RR_editinR.grd 
names       : z 
zvar        : z 

I am very inexperienced with R, so would greatly appreciate any help on a way to achieve this.


Solution

  • First, let's create some dummy data as a 10x10 raster (to make this a reproducible example)

    bath <- raster(nrows=10, ncols=10, vals=rnorm(100))
    

    then we can simply do

    bath[bath>0] <- -10
    

    or, for larger rasters

    bath <- reclassify(bath, cbind(0, Inf, -10))