# [R] Calculate focal values for neighboring cells that are located at the raster edge from the function "focal" in the R package "raster"

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Fri Jun 8 23:52:22 CEST 2018

```The r-sig-geo list is often a better place to post for such
"geographically" related questions, especially if you don't  get a helpful
response  here.

But R is open source, so typing

raster:::focal

at the command line prompt will show you the function code if it is written
in R. Whether that is "helpful" is another matter.

Cheers,
Bert

On Fri, Jun 8, 2018, 9:00 AM Marine Regis <marine.regis using hotmail.fr> wrote:

> Hello All,
>
> I am using the function "focal" in the R package "raster" but I don’t
> understand how the function calculates values for neighboring cells that
> are located at the raster edge. Here is an example reproducible:
>
> f <- matrix(1, nrow=3, ncol=3)
> f[c(1,3,7,9)]=1/sqrt(2)
> f=0
>
> func <- function(x) {
>   sum(abs(x-x)*f)/8
> }
>
> r <- raster(ncol=3, nrow=3)
> vals <- 1:ncell(r)
> r <- setValues(r, vals)
> plot(r)
>
> func_f  <- focal(r, w=matrix(1,nrow=3,ncol=3),
> text(func_f )
> getValues(func_f)
>
>
> From the example, I manage to find the value “2.06066”:
>
> c <- 5
>
> (abs(1-c)*(1/sqrt(2)) + abs(2-c)*1 + abs(3-c)*(1/sqrt(2)) +
>
>   abs(4-c)*1 + abs(5-c)*0 + abs(6-c)*1 +
>
>   abs(7-c)*(1/sqrt(2)) + abs(8-c)*1 + abs(9-c)*(1/sqrt(2))) / 8
>
>
>  but I don’t manage to find the value “2.18566”. How can I find this value
> ? In addition, why does the function with “pad = TRUE” and without
> “pad=TRUE” give the same result ?
>
>  Thank you very much for your time.
>
> Have a nice day
>
> Marine
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help