0

I am trying to apply a code (in R) for all images that I have in a folder. The code works for 1 image, but when I try to make a loop out of it, it gives me error.

library(raster)
library(rgdal)
ET<-"ET"
pre<-"pre"
files <- list.files(path="......", pattern="*.tif", full.names=T, recursive=FALSE)
for (r in files) {
name = r
subname<-sub(ET,pre,r)
srraster<-stack(r,subname)

#identifying some pixels as NA
r[r>30000] <- NA
# creating a function for replacing NA pixels 
fill.na <- function(t) { center = 0.5 + (width*width/2) 
  if( is.na(t)[center] ) { return( round(mean(t, na.rm=TRUE),0) )  } 
else {return( round(t[center],0) )}}  
width = 15
r= raster(focal(r, w = matrix(1,width,width), fun = fill.na, pad = TRUE, na.rm = FALSE))
#Saving files
savename<-sub("res",ET,basename(file))
  writeRaster(r,file=savename)

}
3

The r object in the loop is a string and not a raster object. There are many other errors in your code that would explain why it would crash. First, set up the basics of your code. BTW, you do not need to define a function in a for loop. All you are doing is overwriting it every time the loop iterates.

   library(raster)
   library(rgdal)

    files <- list.files(path="......", pattern="*.tif", full.names=T, recursive=FALSE)

You originally had the argument of this function defined as "t". This is an R internal for transpose and should not be used as a parameter because, depending on how it is interpreted, can cause some very unexpected behavior.

    fill.na <- function(x, width = 15) { 
      center = 0.5 + (width*width/2) 
      if( is.na(x)[center] ) { return( round(mean(x, na.rm=TRUE),0) ) } 
     else { return( round(x[center],0) ) }
    }  

Now, start a for loop, reading each individual raster in the file list. You can use a single bracket index to reference each specific raster via an iterator. This will write a new version of each raster in the files object. I have no idea what you are trying to do with sub but, I am dropping it from the output name. You can figure out how to apply for your specific data. I have the writeRaster output set up so that "new_" is appended to the beginning of the original raster(s) name.

      for (i in 1:length(files)) {
        r <- raster(files[i])
        r[r > 30000] <- NA
        r <- focal(r, w = matrix(1,15,15), fun = fill.na,  
                   pad = TRUE, na.rm = FALSE)
        writeRaster(r, file = paste0("new_", basename(files[i])) )
      }
  • It still gives the same error:" Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘focal’ for signature ‘"character"’ – Seji Jun 5 '18 at 1:39
  • and also this error : Error in compareRaster(rasters) : different extent – Seji Jun 5 '18 at 1:39
  • 1
    Well, one comes before the other. The stack command is failing due to an extent or dimension mismatch. – Jeffrey Evans Jun 5 '18 at 2:59
  • Thank you. it's working. It just overwrites the original file. How I can make them to save somewhere else or to save with a new name, not to overwrite the original files. – Seji Jun 5 '18 at 17:03
  • 1
    You can just append the name using something like: file = paste0("new_", sub("res","ET",basename(files[i]))) – Jeffrey Evans Jun 5 '18 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.