I'm trying to do some simple crop analysis (134 crops) using 14 GCM models, with the data stored within 14 folders. What I want to do is extract the fils for each crop from the 14 models, stack them, analyse them and then repeat for the next crop.
The code I've written so far is:
library(raster)
setwd("/Users/Desktop/JERM/Results/Future/RCP4.5/")
for(i in 1:length(CropsUnique))
{
Cropsi <- EcoID[which(Crops %in% CropsUnique[i])]
for(j in 1:length(ldc))
{
CropSpecies <- stack(paste(ldc[j], "Eco", Cropsi, ".grd", sep="/"))
if(length(Cropsi)>1){CropSpecies <- calc(CropSpecies, function(x) max(x, na.rm=TRUE))}
writeRaster(CropSpecies, paste(ldc[j], CropsUnique[i], ".grd", sep=""), overwrite=TRUE)
}
}
However, I keep getting the following error "Error in file(x, "rb") : cannot open the connection". I assume that the problem is that R can only reach the directory with the folders of the 14 GCMs, but then is unable to then extract the individual crop files from within each folder simultaneously.
I'm sure this is pretty simple, but I just can't for the life of me think how to extract files from separate folders at the same time.