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

Marine Regis m@rine@regi@ @ending from hotm@il@fr
Fri Jun 8 18:00:23 CEST 2018

```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[5]=0

func <- function(x) {
sum(abs(x-x[5])*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]]

```