Yes! It worked. Thank you so much! Best, Anusheema.
On Mon, May 19, 2014 at 1:31 PM, Matteo Mattiuzzi [via R-sig-geo] < ml-node+s2731867n7586468...@n2.nabble.com> wrote: > Is that what you want? > > library(raster) > # your reproducible! raster > r <- raster() > r[] <- sample(0:2,ncell(r),replace=TRUE) > > funX <- function(x){sum(x==1)} > res1 <- focal(r,fun=funX,w=matrix(1, ncol=3, nrow=3)) > # or in this special case (sum if 1) also > rn <- r==1 > res2 <- focal(rn,w=matrix(1, ncol=3, nrow=3)) > > # more general (eg sum if 2): > funX <- function(x){sum(x[x==2])} > res3 <- focal(r,fun=funX,w=matrix(1, ncol=3, nrow=3)) > > Matteo > > > >>> anusheema <[hidden > >>> email]<http://user/SendEmail.jtp?type=node&node=7586468&i=0>> > 05/18/14 8:57 PM >>> > Hi! > > I want to run focal values for neighbourhood of focal cells using a > matrix, > with a function preferably. My data has 0, 1, and 2 as values, and I want > to > add values of "1" only, if I am using the focal function. > > It's probably a very simple question, and I have tried many combinations, > but still not getting the desired results. > > Please help! > > Here's one of the codes that I was attempting: > > # to generate the function > my.function <- function (x) { > ux <- unique(x) > ux [if (x==1) ux <- focal(in_image, w=matrix(1/9, ncol=3, nrow=3)), > fun=sum] > } > > # to run the focal window for the desired function > r_new <- focal(in_image, w=matrix(1/9, ncol=3, nrow=3), > fun=my.function) > > > > -- > View this message in context: > http://r-sig-geo.2731867.n2.nabble.com/Focal-function-in-R-tp7586466.html > Sent from the R-sig-geo mailing list archive at Nabble.com. > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] <http://user/SendEmail.jtp?type=node&node=7586468&i=1> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > > _______________________________________________ > R-sig-Geo mailing list > [hidden email] <http://user/SendEmail.jtp?type=node&node=7586468&i=2> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > > > ------------------------------ > If you reply to this email, your message will be added to the discussion > below: > > http://r-sig-geo.2731867.n2.nabble.com/Focal-function-in-R-tp7586466p7586468.html > To unsubscribe from Focal function in R, click > here<http://r-sig-geo.2731867.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=7586466&code=YW51c2hlZW1hQGdtYWlsLmNvbXw3NTg2NDY2fC0xODk3OTg1ODMw> > . > NAML<http://r-sig-geo.2731867.n2.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> > -- Regards *Anusheema Chakraborty* Ph.D. Scholar Department of Natural Resources, TERI University 10, Institutional Area, Vasant Kunj, New Delhi - 110070 Website: www.teriuniversity.ac.in -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Focal-function-in-R-tp7586466p7586469.html Sent from the R-sig-geo mailing list archive at Nabble.com. _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo