In R, how could I make the distinction between inner and outer NA
's in a raster with some shape having NA
's both around but also inside?
In the example below, how could I for exemple select only the NA
's outside the R logo (i.e., how can I make everything which is included in the circle of the logo appear as white)?
library(raster)
r <- raster(system.file("external/rlogo.grd", package="raster"))
r[r>240] = NA
par(mfrow=c(1,2))
plot(r, main='r')
plot(is.na(r), main="is.na(r)")
You don't really have many options. This type of analysis usually requires some more elaborate methods. Here however is a simple workaround using the clump
function:
#your inital code
library(raster)
r <- raster(system.file("external/rlogo.grd", package="raster"))
rna <- rc <- r
rna[r>240] = NA
par(mfrow=c(2,2))
#reclass values <=240 to NA (needed for clump function.
#Here, NAs are used to seperate clumps)
rc[r<=240] <- NA
rc <- clump(rc)
#what you get after applying the clump function
#are homogenous areas that are separated by NAs.
#Let's reclassify all areas with an ID > 8.
#In this case, these are the areas inside the ring.
rc_reclass <- rc
rc_reclass[rc_reclass>8] <- 100
#let's do some plotting
plot(r, main='r')
plot(is.na(rna), main="is.na(r)")
plot(rc, main="clumps")
plot(rc_reclass, main="clumps reclass")