How to replace NA's in a raster object

ils picture ils · Aug 15, 2012 · Viewed 16.7k times · Source

I need to replace the NA's in the raster object (r) from the example below.

library(raster)
filename <- system.file("external/test.grd", package="raster")
r <- raster(filename)

I also tried to remove these these (and place the result in a data.frame), but to no avail.

dfr <- as.data.frame(r, na.rm=T)
summary(dfr)
# test       
# Min.   : 128.4  
# 1st Qu.: 293.2  
# Median : 371.4  
# Mean   : 423.2  
# 3rd Qu.: 499.8  
# Max.   :1805.8  
# NA's   :6097

Answer

Robert Hijmans picture Robert Hijmans · Feb 16, 2013

A more memory safe approach (for large files) would be to use reclassify:

library(raster)
filename <- system.file("external/test.grd", package="raster")
r <- raster(filename)
rna <- reclassify(r, cbind(NA, 250))