[R-sig-Geo] Raster focal Error with NAonly + na.rm=T, how to select NAonly focal cells?
Robert J. Hijmans
r.hijmans at gmail.com
Fri Sep 5 20:16:42 CEST 2014
Joseph,
The errors go away if you add ellipses to your functions, to let the
na.rm pass through (even if you ignore it).
r3 <- focal (r,w=matrix.weights,fun = function(x,
...){sample(na.omit(x),size=1)},pad=T,padValue=NA,NAonly=T)
The perhaps surprising error with r3 occurs because when NAonly ==
TRUE, na.rm is set to TRUE internally. I will remove that; as it may
not be desirable in all cases.
Robert
On Fri, Sep 5, 2014 at 11:06 AM, Josep M Serra diaz
<pep.bioalerts at gmail.com> wrote:
> 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]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list