0

I have a function that I wrote that looks like this when calling:

myfunction<-(filename_1980, March1980raster.RData, April1980raster.RData, May1980raster.RData, Linkwheretosave="data/filename_1980.RData")

I want to call this function 36 times, but each time change the year so that it is called for each year between 1980-2015.

So instead of calling the function 36 times and manually changing the year in each filename, is there a way to put this into a for loop so that the names get automatically changed with each following year?

For example, I would want the first iteration to run exactly as above, but the next iteration to run as:

myfunction<-(filename_1981, March1981raster.RData, April1981raster.RData, May1981raster.RData, Linkwheretosave="data/filename_1981.RData")

and continue just changing the year until 2015 is reached.

My original function (if it's helpful) is written as :

myfunction<- function(filename_year, Marraster, Aprraster, Mayraster, link){
  
  filename_year<-do.call(c, list(Marraster, Aprraster, Mayraster))
  filename_year<- list(Reduce(`+`, filename_year) / length(filename_year))
save(filename_year, file= here(link))

}

How can I do this in R?

Jamie_B
  • 61
  • 5
  • 1
    [Put your data in a list](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207). Or rewrite your function to accept strings, and then it's trivial to `paste` together the file names, e.g., `arg1 = paste0("filename_", 1980:2016)` – Gregor Thomas Nov 09 '21 at 20:09

0 Answers0