-1

I have around 4000 rasters (tif) all in a single folder. Each tif shows the distribution of a species. The filename of the tif is the species name. I want to make species richness maps from subsets of the species in the folder. I have a csv file with the filenames of the species i want to subset. How do i read into r the subset of tifs based on the filenames listed in a csv? I also need to export the subset of tifs to another folder.

I have done a lot of searching online but can't find anything that helps.

Thanks so much for your help!!

Here are my tif files:

> head(f)
[1] "Abies_alba.tif"            "Abies_cephalonica.tif"     "Abies_nebrodensis.tif"     "Ablepharus_budaki.tif"    
[5] "Ablepharus_chernovi.tif"   "Ablepharus_kitaibelii.tif"

Here is my csv:

> head(csv)
           BINOMIAL
1        Abies_alba
2 Abies_cephalonica
3 Abies_nebrodensis
JoeG
  • 1
  • 1
  • 2
    Welcome to StackOverflow. People around here expect you to show some effort instead of just giving you code.Take the tour: https://stackoverflow.com/tour Please read this https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Since you’re working with rasters read some documentation: https://rspatial.github.io/terra/index.html and do a web search. There are R resources everywhere. https://search.brave.com/search?q=read+data+in+r&source=web – John Polo Feb 18 '23 at 12:57
  • Thanks for this John, i have already done a lot of searching online (have updated the question to make this clear) but can't find anything to help – JoeG Feb 18 '23 at 23:19
  • Please edit your question. Use `f <- list.files` and show us (`head(f)`. Likewise read the csv file with `read.csv` and use head to show some of its contents. You can extract the species names from the fiilenames and match these to the names in the data.frame. But I need example data to show that. – Robert Hijmans Feb 19 '23 at 18:20
  • I have updated the question with this information. Thanks so much for your help! – JoeG Feb 20 '23 at 03:11

2 Answers2

0

You can loop over the values stored in the csv and use the paste0 function to make the paths:

library(raster)
library(ggplot2)
library(rasterVis)

origin_folder <- "C:/origin/"
destination_folder <- "C:/destination/"
csv_list <- data.frame(files = c("filename1", "filename2"))

# Loop over each filename 
for (i in csv_list$files){
  # Read the file from the origin folder
  specie <- raster(paste0(origin_folder, i, ".tif"))

  # Plot the heatmap
  gplot(specie) + geom_tile(aes(fill = value))
 
  # Save the file in the destination folder
  writeRaster(specie, paste0(destination_folder, i, ".tif"))
}
L--
  • 565
  • 1
  • 12
  • Thanks for this, this is exactly what i was after. It doesn't seem to work with the 'plot the heat map' part but when i remove that it works! I had to add underscores into the csv. Will add the answer below – JoeG Feb 21 '23 at 02:50
0

Using the help below, this was the code that worked:

library(raster)

origin_folder <- "C:/origin/"
destination_folder <- "C:/destination/"
csv <- read.csv("C:/CSV location.csv")

# Loop over each filename 
for (i in csv$BINOMIAL){
  # Read the file from the origin folder
  specie <- raster(paste0(origin_folder, i, ".tif"))
  
  # Save the file in the destination folder
  writeRaster(specie, paste0(destination_folder, i, ".tif"))
}
L--
  • 565
  • 1
  • 12
JoeG
  • 1
  • 1