# [R] How to calculate the moving average for binary files?

David Winsemius dwinsemius at comcast.net
Mon Feb 18 22:11:50 CET 2013

```On Feb 18, 2013, at 12:28 PM, Jonsson wrote:

> I have 12 binary (raster) files
> https://echange-fichiers.inra.fr/get?k=k3M2jatJyHy65Cs99G4 .
> I would like to calculate the moving average for the 12 values for each
> pixel in the 12 files.
>
> For a simple vector we can get a moving average by using this :
>
>             x <- c(1,2,3,NA,NA,4,6,5,6,4,2,5)
>            movingmean <- rollapply(x, 3, FUN = mean, na.rm = T,fill=NA)
> now I want to do the same but with binary files and I tried:
>
>    files   <- list.files("C:final-2010", "*.envi", full.names = TRUE)
>    results <- list()
>    for (.files in files) {
>        # read in the 12 files as a vector of numbers
>        # that we take the average of
>        x <- do.call(rbind,(lapply(.files, readBin  , double() ,
>                         size = 4 ,n =1440 * 720 , signed = T)))
>        # take the moving average across the 12 values
>        # from the 12 files for each pixel
>        results[[length(results) + 1L]] <- rollapply(x, 3, FUN = mean,na.rm
> = T)
>    }
>
> But got this error:
>
>        Error in seq.default(start.at, NROW(data), by = by) :

Cross-posting on StackOverflow and Rhelp is deprecated in the Posting Guide.

http://stackoverflow.com/questions/14944824/how-to-calculate-the-moving-averag-for-several-binary-files-in-r

--

David Winsemius
Alameda, CA, USA

```