I am new in R.

How can I replace NAs with 0 for my raster data which have spatial information?

  • 3
    Does this answer your question? Replace NA's with 0 for large raster data using R?. I suggesting it to become a dupe from newer version (which addresses other/better options for large datasets). Moreover, OP there linked in Q my answer here; so that question's starting point is this Q/A answer anyway. I think it improves searchability. Commented Nov 6, 2021 at 15:35

1 Answer 1


The answer from Wilfried Thuiller in R-SIG-Geo mailing list works:

#getting a raster
f <- system.file("external/test.grd", package="raster")
r <- raster(f) #r is the object of class 'raster'.

# replacing NA's by zero
r[is.na(r[])] <- 0 

Not the answer you're looking for? Browse other questions tagged or ask your own question.