0

So I'm trying to build a raster stack of 7 variables. In ArcMap I've already standardised resolution and CRS, and I tried to standardise extent and column/row number by extracting by mask with a polygon of the study area. Despite this, extent and column/row number still varies.

Originally I skipped over this issue using quick=TRUE when building the raster stack, but since doing that will create more problems later I decided to try to sort it out now. I've tried using the solution posted here, and ended up with this code:

var_files <- list.files(file.path("D:/OneDrive/Diss/final_layers/"),
                        pattern="tif$", full.names = TRUE)
r.var <- list()
for(i in var_files) {
  ras <- try( raster(i) )
  r.var[i] <- data.frame(name = i, nrow=nrow(ras), ncol=ncol(ras), res=res(ras)[1],               
                         proj=proj4string(ras), xmin=extent(ras)[1],             
                         xmax=extent(ras)[2], ymin=extent(ras)[3],                     
                         ymax=extent(ras)[4])
}
do.call("rbind", var_files)
var_stack <- stack(var_files)
plot(var_stack)

And then I get these warnings after the loop:

In addition: Warning messages:
1: In r.var[i] <- data.frame(name = i, nrow = nrow(ras), ncol = ncol(ras),  :
  number of items to replace is not a multiple of replacement length
2: In r.var[i] <- data.frame(name = i, nrow = nrow(ras), ncol = ncol(ras),  :
  number of items to replace is not a multiple of replacement length
3: In r.var[i] <- data.frame(name = i, nrow = nrow(ras), ncol = ncol(ras),  :
  number of items to replace is not a multiple of replacement length
4: In r.var[i] <- data.frame(name = i, nrow = nrow(ras), ncol = ncol(ras),  :
  number of items to replace is not a multiple of replacement length
5: In r.var[i] <- data.frame(name = i, nrow = nrow(ras), ncol = ncol(ras),  :
  number of items to replace is not a multiple of replacement length
6: In r.var[i] <- data.frame(name = i, nrow = nrow(ras), ncol = ncol(ras),  :
  number of items to replace is not a multiple of replacement length
7: In r.var[i] <- data.frame(name = i, nrow = nrow(ras), ncol = ncol(ras),  :
  number of items to replace is not a multiple of replacement length

And this error after do.call:

Error in do.call("rbind", var_files) : second argument must be a list
2
  • your error message refers to the fact that var_files is not a list. Should not it be do.call("rbind", r.var) (the list you produce in the loop)? – Bruno Conte Leite Jun 3 '20 at 15:32
  • Ahh, nice catch - although they still have different extents, so can't make a stack – Xandian97 Jun 3 '20 at 16:31
0

In the end the solution was absurdly simple - I just made the stack using quick=TRUE, then cropped the stack rasters by the extent of an ESRI shapefile I'd converted to a SpatialPolygonDataFrame. So after the conversion the code was simply:

crop_area <- extent(poly)
var_brick <- crop(var_stack, crop_area)

This gave me a RasterBrick which I could plot without any errors, so I assume it should be fine now.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.