1

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.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
Arferion
  • 53
  • 4
  • Please see [how to make a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). There are several things going on making this not reproducible. – alexwhitworth Feb 23 '16 at 18:45
  • You really need to make clear that `stack` and `calc` come from the `raster` package. `stack` is also a base function. – MichaelChirico Feb 23 '16 at 18:46
  • 3
    some combination of `lapply` and `list.files(recursive = TRUE)` should get you started. – MichaelChirico Feb 23 '16 at 18:50

0 Answers0