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
var_files
is not a list. Should not it bedo.call("rbind", r.var)
(the list you produce in the loop)?