0

I have a DEM for a big region. And I also have a vector-file for several sub-regions. I want to create as many DEM's as I have sub_regions. And I would like to do this in a for loop and for example create dem_1, dem_2,.... automatically. And I guess in the best case I'd store these RasterLayer's in a vector. What I don't know is how to continually change the name of the dem to dem_1, dem_2 etc. in the loop. And how to store them in a vector I'm really not too familiar with loops in R as they kind of scare me...;) My embarrassing current approach looks something like this:

crop_dem_to_polys = function(dem, vector){
  dems = numeric(length = nrow(vector)
  for (i in length(vector)){
    dem + "_i" = crop(dem, vector[i, ]) #looks really wrong dem+"_i"
    append(dems, dem + "_i")
  }
}

I guess there'd be some option with lapply (or just apply), but I really don't know.

  • 1
    Use a list. Initialise dems=list() outside the loop and assign dems[[i]] = crop(..etc..) inside. lapply will do most of this. Find a nice base R tutorial. – Spacedman Feb 15 at 10:31

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.