简体   繁体   中英

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.

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

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM