How to replace NA's in a raster object
Asked Answered
B

2

16

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
Blimey answered 15/8, 2012 at 8:55 Comment(5)
Yes, summary(as.data.frame(r, na.omit=T)) says that there are 6097 NA's.Blimey
Welcome to StackOverflow. +1 for reproducible example.Maddeu
As you can see in the answer, na.omit is a function not an argument of as.data.frame.Ghana
I saw that, @seancarmody, thanks!Blimey
Try SpatialPixelsDataFrame for sparse grids without the NAs, but this is pretty open ended, what do you really need?Urania
M
17

I'm not sure it makes sense to remove NA values from a raster object, but you can easily replace it.

For example:

oldpar <- par(mfrow=c(1, 2))
plot(r)
r[is.na(r)] <- 250
plot(r)
par(oldpar)

enter image description here


If you really want to, you can extract the raster values into a vector and then remove the NA values. (Although, since you lose the spatial information, I can't see how this can be helpful.)

r <- raster(filename)

r <- values(r)
head(r)
[1] NA NA NA NA NA NA

head(na.omit(r))
[1] 633.686 712.545 654.162 604.442 857.256 755.506
Maddeu answered 15/8, 2012 at 9:31 Comment(0)
A
31

Here are three ways in which you can do this with terra

library(terra)
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
x <- classify(r, cbind(NA, -99))
y <- subst(r, NA, -99)
z <- ifel(is.na(r), -99, r)

With raster, the best (memory safe approach that also works for large files) is to use reclassify:

library(raster)
filename <- system.file("external/test.grd", package="raster")
r <- raster(filename)
rna <- reclassify(r, cbind(NA, 250))
Anaclitic answered 16/2, 2013 at 7:20 Comment(5)
This doesn't seem to work for NaN (i.e. reclassify(r, cbind(NaN, NA)))Gogh
Using the "in-place" function set.values, we can also write: set.values(r, which(is.na(values(r))), -99), which is perhaps the fastest way to substitute values in a terra raster.Marable
That may be, but using values is not memory safe, so I won't recommend it.Anaclitic
What exactly do you mean by "not memory-safe"? Are you talking about the fact that all the other rasters pointing to the modified raster are also modified? Or is there another danger?Marable
values attempts to read all raster cell values into memory. That may not be possible.Anaclitic
M
17

I'm not sure it makes sense to remove NA values from a raster object, but you can easily replace it.

For example:

oldpar <- par(mfrow=c(1, 2))
plot(r)
r[is.na(r)] <- 250
plot(r)
par(oldpar)

enter image description here


If you really want to, you can extract the raster values into a vector and then remove the NA values. (Although, since you lose the spatial information, I can't see how this can be helpful.)

r <- raster(filename)

r <- values(r)
head(r)
[1] NA NA NA NA NA NA

head(na.omit(r))
[1] 633.686 712.545 654.162 604.442 857.256 755.506
Maddeu answered 15/8, 2012 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.