0

I have a folder with many csv files. Each file has several columns as well as lat and long columns. Another folder have many rasters in tif format. The .csv files are named based on Julian date (e.g. 251.csv), and so the rasters (e.g. 251.tif). I would like to be able to add the raster value to the csv with matching name and save to a new csv in R. What I want to achieve is this:

raster<-raster("c:/temp/TIFF/2001/273.tif")
points<-read.csv("c:/temp/csv/2001/273.csv")
coordinates(points)=~long+lat
rasValue=extract(raster,points)
combinePointValue <- cbind(points,rasValue)
head(combinePointValue)
library(spdplyr)
combinePointValue <- combinePointValue %>% 
  rename(chloro = 10)
write.table(combinePointValue,file="c:/temp/2001/chloro/273_chloro.csv",append=FALSE, 
sep=",",row.names=FALSE, col.names=TRUE)

Considering the many csv and many tif files, I would prefer avoiding having to type this over and over.

Anyone able to help?

2
  • Write this code into a function that takes one parameter, the day number, then constructs the file names based on the number. Then loop over the days and call the function with that day. If none of that is any help, you should find an R tutorial on writing functions and programming.
    – Spacedman
    Commented Jun 20, 2022 at 9:52
  • Thank you Spacedman. Will try following your advise, not experienced with writing functions!
    – Ilaria
    Commented Jun 20, 2022 at 13:33

1 Answer 1

0

Here's a function (untested) that takes a number and makes the paths and does the work:

process_one = function(d){

folder = "c:/temp/TIFF/2001"
rasterinput = file.path(folder, paste0(d,".tif"))
pointsinput = file.path(folder, paste0(d,".csv"))

output = file.path("C:/temp/2001/chloro/", paste0(d,"_chloro.csv"))

message("Reading from ",rasterinput, " and ",pointsinput)
message("Writing to ",output)

raster<-raster(rasterinput)
points<-read.csv(pointinput)
coordinates(points)=~long+lat
rasValue=extract(raster,points)
combinePointValue <- cbind(points,rasValue)

## is this what that mutate pipeline is doing? 
combinePointValue$chloro = 10

write.table(combinePointValue,file=output,append=FALSE, 
sep=",",row.names=FALSE, col.names=TRUE)

message("...done")

}

Then you loop:

for(dd in 200:250){
 process_one(dd)
}

which will say stuff like this (I don't actually have these files, so this is a smaller function that just prints the names):

Reading from c:/temp/TIFF/2001/200.tif and c:/temp/TIFF/2001/200.csv
Writing to C:/temp/2001/chloro//200_chloro.csv
Reading from c:/temp/TIFF/2001/201.tif and c:/temp/TIFF/2001/201.csv
Writing to C:/temp/2001/chloro//201_chloro.csv
2
  • Hello Spacedman, thank you for trying to help. It now says that my tif files do not exist. Can I perhaps share 3 .csv and 3 corresponding tif files for you to test?
    – Ilaria
    Commented Jun 23, 2022 at 10:40
  • If R is saying the tif files don't exist then R is looking in the wrong place. When it prints the path out, is it correct? Is it where you expect the file to be? Does R's file.exists(rasterinput) return TRUE or FALSE? Edit your Q and make some changes to update us with your current problem, because I think in principle my code should work, so we are missing some detail.
    – Spacedman
    Commented Jun 23, 2022 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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