[R-sig-Geo] Raster focal Error with NAonly + na.rm=T, how to select NAonly focal cells?

Josep M Serra diaz pep.bioalerts at gmail.com
Fri Sep 5 20:06:47 CEST 2014


Dear listers,

I have been trying to understand why I am getting this Error when using the
focal function

Error in fun(x, na.rm = TRUE) : unused argument (na.rm = TRUE)



I am trying to build an iteration where NA values in a raster are
substituted by a random sample from the focal window (3x3 window).

That is, my center NA value would take random values of the 3x3 window
without considering NAs
I tried to reproduce an example here


Example:

library (raster)

a <- matrix (c(1,2,4,5,
               2,NA,NA,1,
               3,2,4,5,
               2,3,4,NA,
               NA,2,1,3),ncol=4)

r <-raster (a)
matrix.weights <- matrix(rep(1,times=9),ncol=3)
matrix.weights

r2 <- focal (r,w=matrix.weights,fun = function(x){sample(x,size=1)}, pad=T,
padValue=NA, NAonly=T, na.rm=T)

Error in fun(x, na.rm = TRUE) : unused argument (na.rm = TRUE)


The error likely makes sense here because the function sample does not
accept na.rm argument,


I also tried
r3 <- focal (r,w=matrix.weights,fun = function
(x){sample(na.omit(x),size=1)},pad=T,padValue=NA,NAonly=T)

Error in fun(x, na.rm = TRUE) : unused argument (na.rm = TRUE)

withouth the NAonly argument it does work, however
r4 <- focal (r,w=matrix.weights,fun = function
(x){sample(na.omit(x),size=1)},pad=T,padValue=NA)


How can I make it to select only NAonly cells for focal? I have a pretty
big raster that doing it without NAonly=T can be ...slow

Thanks,

Josep

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list