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]])
  writeRaster(timePred, "xxxxx.tif")

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?

  • 1
    I've chopped this down to your first question - if you want to ask the other questions put them in new posts, that way we have (ideally!) one question and one answer per post! Your extra question text should be visible in the edit history if you want to recover it. – Spacedman Oct 31 '20 at 12:10
  • Thanks @Spacedman ! I'll make another post for the other question. – Joanne Bentley Oct 31 '20 at 12:24

The raster package is very strict about names, it seems, and really doesn't want you to have two layers in a stack with the same names. Weirdly you can make a plain R list with two elements with the same name:

> L = list(a=1, a=2)
> L$a
[1] 1
> names(L)
[1] "a" "a"

but the only way to get the second one is with L[[2]].

But the raster package goes through a lot of effort to stopping you from doing this.

I think you'll have to pull each layer out and name it individually as you go through the loop. For example:

for (i in 1:94){
  stackedRast_i = stackedRast[[i]]
  names(stackedRast_i) = "bio1"
  timePred <- predict(G15.gdm.1, rastFile, time=TRUE,

There's a neater way of doing this via setNames which returns a named version of its argument:

> s1 = setNames(s[[1]], "bio1")
> names(s1)
[1] "bio1"

So your loop can be:

 for (i in 1:94){
    timePred <- predict(G15.gdm.1, rastFile, time=TRUE,

Note this doesn't change the names of stackedRast, it returns a raster layer with the name set on it.

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.