I would like to replace the NA cells that are within a specified extent (here rectangular area defined by
drawExtent in the code bellow) of a raster object. My idea was to use the functions
extract to extract the NA cells within an extent of the raster and to assign the value 250 to these cells. Finally, the NA cells of the raster that are within the extent should have the value 250 and the other NA cells that are outside the extent would keep the value NA. So, How can I change the values of NA cells in a raster object by using a geographic subset of the raster as a condition ? Here is the beginning of a code:
r <- raster(ncol=10, nrow=10) values(r) <- sample(1:8,ncell(r),replace=T) r[c(5)] <- NA r[c(20)] <- NA r[c(43)] <- NA plot(r) e <- drawExtent() z <- extract(r, cellsFromExtent(r, e))
z, how can I assign the value 250 to the NA cells in the raster (I tested
r[is.na(z)] <- 250 but this doesn't work) ?