[R] How write raster files after manipulation?

Jonsson amen.alyaari at Bordeaux.inra.fr
Thu Feb 14 13:22:19 CET 2013


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)
now I want to do the same but with rasters and I tried:

    files   <- list.files("C:final-2010", "*.envi", full.names = TRUE)
    results<- overlay(stack(files), fun=function(x) movingFun(x, fun=mean,
n=3, na.rm=TRUE))

That worked without errors but I want to write the results and check them:
I tried several ways of writing but all of them got errors:
        
             1)for (i in seq_along(results)) {
       fileName <- sprintf("C:\\New folder (3)\\final-2010.bin", i)
       writeBin(as.double(results[i]), fileName, size = 4)
       }
      Error in writeBin(as.double(results[i]), fileName, size = 4) : 
      (list) object cannot be coerced to type 'double'


      2) for (i in seq_along(results)) {
       fileName <- sprintf("C:\\New folder (3)\\final-2010.bin", i)
       writeBin(as.double(results[[i]]), fileName, size = 4)
          }
      Error in as.double(results[[i]]) : 
      cannot coerce type 'S4' to vector of type 'double'

       3) for(i in 1:length(results)){
           fileName <- strsplit(results[i],split='\\.')[[1]][1]
       outputFile <- paste(fileName,'_amenlast','.envi',sep='')
       rf <- writeRaster(results, filename=outputFile, overwrite=TRUE)
        }
       Error in strsplit(results[i], split = "\\.") : non-character argument
       to write the results:

Any ideas?

   



--
View this message in context: http://r.789695.n4.nabble.com/How-write-raster-files-after-manipulation-tp4658539.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list