1

I have a lot (>50) of SpatialGridDataFrame files that I need to turn into raster files, stack with a set of raster files I already have and then extract data from random points from each layer of all of these individual stacks. I have written the code to do this for each file at a time and it works fine but I was trying to use a loop so I could run this for all of the SpatialGridDataFrame files in one go rather than one at a time.

I wasn't successful at listing the SpatialGridDataFrame files together to use as an input into the loop and get the following (example just with a few of my original files);

Layer1 <- list(SpatialGridDataFrame1A,SpatialGridDataFrame2A,SpatialGridDataFrame3A,SpatialGridDataFrame4A)))
Layer2 <- list(SpatialGridDataFrame1B,SpatialGridDataFrame2B,SpatialGridDataFrame3B,SpatialGridDataFrame4B)))

But this doesn't work correctly and when I try to call up the lists they look like this;

> Layer1
[[1]]
class       : SpatialGridDataFrame 
dimensions  : 11, 44, 484, 2  (nrow, ncol, ncell, nlayers)
resolution  : 0.01788854, 0.01788854  (x, y)
Error in .printSpatial(x) : object 'nf' not found

Is there a way of listing these files together successfully so that I can then run the following loop?

for (i in 1:4)
 {
  L1 <- raster(Layer1[i])
  L2 <- raster(Layer2[i])
  depth <- raster(Depth_int)

rasterratio<- (L1/L2)

stack_all <- stack(L1,L2,depth,rasterratio)
assign( paste("layers",i,sep="_"), stack_all) 
}

Or maybe use some other function like lapply to achieve this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.