0

I have a SpatialPixelsDataFrame (sp object) for which I wish to remove all pixels containing NA values in the @data slot. I am aware of this post but the function throws an error. The final result should equally be a SpatialPixelsDataFrame.

Here is a reproducible example:

library(sp)
library(sf)
library(raster)
library(SpatialEco)

m <- raster::raster(ncol=100, nrow=100) # create raster
m[] <- runif(ncell(m))
m[m < 0.2] <- NA # simulate NAs
spdf <- as(m, "SpatialPixelsDataFrame") # transform to SpatialPixelsDataFrame

spdf_new <- sp.na.omit(st_as_sf(spdf), margin = 1)
Warning message
Function is deprecated because stats::na.omit operates on 
  sf class objects

Thanks in advance!

jlklein
  • 23
  • 5
  • I think the `data` slot object will be stored in a matrix. It makes no sense to "remove" NA values from an entry in a grid structure. There's either a value in a location or there is an NA. – IRTFM May 16 '23 at 17:28
  • @IRTFM you are right. thank you for the explanation! – jlklein Jun 12 '23 at 09:33

0 Answers0