4

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.

Psidom
  • 195,464
  • 25
  • 298
  • 322
Archelon
  • 43
  • 7

2 Answers2

8

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
Community
  • 1
  • 1
dww
  • 28,320
  • 5
  • 57
  • 99
4

Here is a memory-safe variation on dww's answer:

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

rbath <- reclassify(bath, cbind(0, Inf, -10), filename='file.grd')
Robert Hijmans
  • 31,122
  • 4
  • 43
  • 51