Remove element from list

How can I remove the element called ntl.tif, from a list in the below code?

wd = "C:/Users/nikos/OneDrive/Desktop/tehran/"

ntl = rast(paste0(wd, "ntl.tif"))

v <- vect(paste0(wd, "lc.shp"))

doStuff <- function(file){
  
  pic = rast(file)
  
  for (i in seq(from = 0.3, to = 3, by = 0.1)) {
    
    print(i)
    
    gf <- focalMat(pic, i * res(ntl), "Gauss")
    r_gf <- focal(pic, w = gf, na.rm = TRUE)
    
    r_gf = exact_resample(r_gf, ntl, fun = 'mean', coverage_area = FALSE)
    
    r_gf <- mask(r_gf, v)
    # ext(r_gf) <- ext(ntl)
    
    (stringedi = gsub("\\.", "", toString(format(i, nsmall = 2))))
    
    writeRaster(r_gf, 
                paste0(wd, 
                       basename(fs::path_ext_remove(file)),
                       stringedi, ".tif"), 
                overwrite=TRUE)
  }
  
}

list.files(wd, pattern = "tif$", full.names = TRUE) |>
  purrr::walk(doStuff)

I tried something like

list.files(wd, pattern = "tif$", full.names = TRUE != "ntl.tif") |>
  purrr::walk(doStuff)

or with an if statement

doStuff <- function(file){
  if (file != file.path(wd, "ntl.tif")) {
    pic = rast(file)
  
    for (i in seq(from = 0.3, to = 3, by = 0.1)) {
      
      print(i)
      
      gf <- focalMat(pic, i * res(ntl), "Gauss")
      r_gf <- focal(pic, w = gf, na.rm = TRUE)
      
      r_gf = exact_resample(r_gf, ntl, fun = 'mean', coverage_area = FALSE)
      
      r_gf <- mask(r_gf, v)
      ext(r_gf) <- ext(ntl)
      
      (stringedi = gsub("\\.", "", toString(format(i, nsmall = 2))))
      
      writeRaster(r_gf, 
                  paste0("path/", 
                        basename(fs::path_ext_remove(file)),
                        stringedi, ".tif"), 
                  overwrite=TRUE)
    }
  }
}

but I couldn't fix the issue.

wd = "path/"

ntl = rast(paste0(wd, "ntl.tif"))

v <- vect(paste0(wd, "lc.shp"))

doStuff <- function(file){
  
  pic = rast(file)
  
  for (i in seq(from = 0.3, to = 3, by = 0.1)) {
    
    print(i)
    
    gf <- focalMat(pic, i * res(ntl), "Gauss")
    r_gf <- focal(pic, w = gf, na.rm = TRUE)
    
    r_gf = exact_resample(r_gf, ntl, fun = 'mean', coverage_area = FALSE)
    
    r_gf <- mask(r_gf, v)
    ext(r_gf) <- ext(ntl)
    
    (stringedi = gsub("\\.", "", toString(format(i, nsmall = 2))))
    
    writeRaster(r_gf, 
                paste0(wd, 
                       basename(fs::path_ext_remove(file)),
                       stringedi, ".tif"), 
                overwrite=TRUE)
  }
  
}

files <- list.files(wd, pattern = "tif$", full.names = TRUE)
files <- files[-(2)] # the number represents the image that should be dropped from the list
purrr::walk(files, doStuff)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.