# Calculate focal values for neighboring cells that are located at the raster edge from the function "focal" in the R package "raster" Classic List Threaded 3 messages Reply | Threaded
Open this post in threaded view
|

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

 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),                    fun= func, pad = TRUE, padValue = NA) 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

 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 <[hidden email]> 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), >                    fun= func, pad = TRUE, padValue = NA) > 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]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. Is > helpful is >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

 Hello, >> 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: focal is a method for the S4 object RasterLayer. You can have access to this method by using > findMethods("focal") and you can trace the method by using > trace(what='focal', tracer=browser, at=1, signature='RasterLayer') HTH, Jeremie >> } >> >> 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), >>                    fun= func, pad = TRUE, padValue = NA) >> 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]] >> >> ______________________________________________ >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help>> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html>> and provide commented, minimal, self-contained, reproducible code. Is >> helpful is >> > > [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.