Locate raster files based on their names, iteratively apply a function to each raster and export a data frame using as name 'some name' plus part of the name of that raster

In a folder containing a lot of raster files, I want to select only the rasters with contain a specific name. For example, if the folder contains 3 rasters, x.tif, gwr_pred20.tif, gwr_pred30.tif, I want to select only the tif files with the name gwr_pred. Then I want to convert these 2 rasters into a data.frame with 3 columns, the x and y coordinates and the pixel values. Then, I want to remove the column containing the pixel values and lastly I want export the data.frame as csv with name coords20.csv (if I use the gwr_pred20.tif to perform this analysis), coords25.csv (if I use the gwr_pred25.tif for this analysis).

I can do this manually:

library(raster)

ebbi = raster("path/gwr_pred20.tif")
vals_ebbi <- as.data.frame(values(ebbi))
ebbi_coords = as.data.frame(xyFromCell(ebbi, 1:ncell(ebbi)))
combine <- as.data.frame(cbind(ebbi_coords,vals_ebbi))
combine <- na.omit(combine)
names(combine)[3] <- "lj"
combine <- subset(combine, select = -lj)
write.csv(combine, "path/coords20.csv", 
          row.names = F)

Here are 3 rasters, for simplicity:

x = raster(ncols=113, nrows=84, xmn=3814073.4192, xmx=3870573.4192, ymn=3255211.6674, ymx=3297211.6674, crs='+proj=lcc +lat_0=24 +lon_0=80 +lat_1=12.472955 +lat_2=35.1728044444444 +x_0=4000000 +y_0=4000000 +datum=WGS84 +units=m +no_defs')

gwr_pred20 = raster(ncols=113, nrows=84, xmn=3814073.4192, xmx=3870573.4192, ymn=3255211.6674, ymx=3297211.6674, crs='+proj=lcc +lat_0=24 +lon_0=80 +lat_1=12.472955 +lat_2=35.1728044444444 +x_0=4000000 +y_0=4000000 +datum=WGS84 +units=m +no_defs')

gwr_pred25 = raster(ncols=113, nrows=84, xmn=3814073.4192, xmx=3870573.4192, ymn=3255211.6674, ymx=3297211.6674, crs='+proj=lcc +lat_0=24 +lon_0=80 +lat_1=12.472955 +lat_2=35.1728044444444 +x_0=4000000 +y_0=4000000 +datum=WGS84 +units=m +no_defs')

But I would like to automate the process because I have >30 gwr_pred.tif files and many more other rasters with random names.

library(raster)

rlist = list(list.files(path = "path", 
                        pattern = "^gwr_pred\\d+\\.tif$", 
                        all.files = T, 
                        full.names = F))


for (i in rlist) {
  for (j in i) {
    print(j)
    getGwrPredName = gsub(".tif", "", j)
    getGwrPredNum = gsub("\\D+","",j)
    finalName = paste("path/coords", getGwrPredNum, ".csv", sep = "")
    ebbi = raster(paste("path/gwr_pred", getGwrPredNum, ".tif", sep = ""))
    vals_ebbi <- as.data.frame(values(ebbi))
    ebbi_coords = as.data.frame(xyFromCell(ebbi, 1:ncell(ebbi)))
    combine <- as.data.frame(cbind(ebbi_coords, vals_ebbi))
    combine <- na.omit(combine)
    names(combine)[3] <- "lj"
    combine <- subset(combine, select = -lj)
    write.csv(combine, finalName,
              row.names = F)
  }
}

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.