[R] How to get the rolling standard deviation in rasters?

Peter Ehlers ehlers at ucalgary.ca
Tue Mar 19 16:15:34 CET 2013


On 2013-03-19 06:41, Jonsson wrote:
> I am using this code to calculate the moving average mean.It worked fine but
> when I wanted to also calculate based on sd(stander deviation) I got the
> error shown below.
> I read this documentation of R movingFun and found that sd was mentioned at
> http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/raster/html/movingFun.html
>
> I wonder if needs any other things to conseder or sd is not at all supplied
> by this function.
>
>         sami<- list.files("C:\\Usersfinal-2010", "*.envi", full.names = TRUE)

We're not sitting at your computer, so the above line is useless 
information.

>          saf=stack(sami)
>          dm <-  overlay(saf ,fun=function(x) movingFun(x, fun=mean, n=3,
> na.rm=TRUE,fill=NA))
> ### worked very well
>
>             ds <-  overlay(saf ,fun=function(x) movingFun(x, fun= sd, n=3,
> na.rm=TRUE))
> ### did not work
>
>             Error in .overlayList(x, fun = fun, filename = filename, ...) :
>               cannot use this formula, probably because it is not vectorized

   [...snip...]

This suggests that it might be useful to read ?Vectorize.

Peter Ehlers



More information about the R-help mailing list