Identifying value of closest non-NA pixel

I have a raster representing a large landscape, where pixel values indicate particular land classes. I have been using the "distance" function in Package 'raster' in R to calculate the distance from all NA pixels to the nearest pixel with a value. In addition to the distance, I'm interested in knowing what the value was associated with the distance calculation. For example, if I have two land class values, e.g. 1= "Urban" and 2= "Agriculture", I'd like to know if a pixel is closest to an "Urban" pixel or an "Agriculture" pixel. I looked into the source code for the distance function, which converts the pixels to points and then calculates all relative distances but only returns the shortest distance. See below for an example of the distance function:

library(raster)
r <- raster(ncol=36,nrow=18)
r[] <- NA
r[500] <- 1
r[100] <-2
dist <- distance(r) #calculates the distance to the nearest non-NA pixel

Is there a function in a different package that does this (I'd like to stay in the R environment)?

• In many cases there will be multiple nearest pixels. Wouldn't you want to know about this, and to know how many of those pixels are in each class? – whuber Mar 20 '16 at 23:09
• @whuber...Insightful comment. It would be helpful though in my particular case I would likely favor one land cover type over another. – KevinB Mar 30 '16 at 23:06

You can do something along these lines:

Example data:

library(raster)
r <- raster(ncol=10,nrow=10, ext=extent(c(0,10,0,10)), crs='+proj=utm +zone=1')
r[] <- 1
r[15:42] <- 2
set.seed(0)
r[sample(ncell(r), 50)] <- NA

Create separate layers for each class:

class1 <- reclassify(r, cbind(2, NA))
class2 <- reclassify(r, cbind(1, NA))

Compute the distance to each class:

dist1 <- distance(class1)
dist2 <- distance(class2)
s <- stack(dist1, dist2)
names(s) <- c('c1', 'c2')

Now you can extract by xy coordinates (or alternatively, cell numbers):

extract(s, cbind(3,4))

#     c1       c2
#[1,]  0 2.828427

For this location, the distance to 'c1' is zero (that is, the cell is class1) and the distance to the nearest cell of class2 is 2.8

If you want a RasterLayer, you can do:

x <- which.min(s)

Note that there will be ties, and that the first layers is taken. This affects the following cells:

equals <- dist1 == dist2

That aside, to only see the nearest value for the NA cells: