I have a for loop where I loop layers from a rasterstack into a predict function, which requires the output of a model and two raster files, including one layer for the present ("rastFile") and one stack or brick for the future ("stackedRast"), as follows:
for (i in 1:94){
timePred <- predict(G15.gdm.1, rastFile, time=TRUE, predRasts=stackedRast[[i]])
stack(timePred,stackedRast[[i]])
writeRaster(timePred, "xxxxx.tif")
rm(timePred)
}
First of all, for the predict function to work, I need all the layers in the rasterstack to have the same name, for instance "bio1"
for (i in 1:nlayers(stacked)) {names(stacked[[i]]) <- "bio1"}
However, R adds a number to the end of the layer names, like this:
> stackedRast
class : RasterStack
dimensions : 1200, 2760, 3312000, 4 (nrow, ncol, ncell, nlayers)
resolution : 0.04166667, 0.04166667 (x, y)
extent : -180, -65, 35, 85 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
names : bio1.1, bio1.2, bio1.3, bio1.4
min values : -24.70417, -22.44583, -19.79583, -16.23750
max values : 25.96667, 27.47917, 28.76250, 30.28750
Is there a way to get around this? (P.S. I've just included a smaller rasterstack of 4 layers for the example, but my final rasterstack will have 94 layers).
Does anyone please have a suggestion for how I can elegantly create one process (loop or otherwise) for changing the layer names so that they all match?