0

I'm trying to change the names of a RasterStack but the result is not changed. Below are the code lines with comments:

So, here's a simple loading of my .tif files

#Loading predictors' extent/mask
preds <- list.files("output/", pattern = ".tif", full.names = TRUE)
preds
 [1] "output/CHELSA_bio01_1981-2010_V.2.1.tif"           "output/CHELSA_bio02_1981-2010_V.2.1.tif"          
 [3] "output/CHELSA_bio03_1981-2010_V.2.1.tif"           "output/CHELSA_bio04_1981-2010_V.2.1.tif"          
 [5] "output/CHELSA_bio05_1981-2010_V.2.1.tif"           "output/CHELSA_bio06_1981-2010_V.2.1.tif"          
 [7] "output/CHELSA_bio07_1981-2010_V.2.1.tif"           "output/CHELSA_bio08_1981-2010_V.2.1.tif"          
 [9] "output/CHELSA_bio09_1981-2010_V.2.1.tif"           "output/CHELSA_bio10_1981-2010_V.2.1.tif"          
[11] "output/CHELSA_bio11_1981-2010_V.2.1.tif"           "output/CHELSA_bio12_1981-2010_V.2.1.tif"          
[13] "output/CHELSA_bio13_1981-2010_V.2.1.tif"           "output/CHELSA_bio14_1981-2010_V.2.1.tif"          
[15] "output/CHELSA_bio15_1981-2010_V.2.1.tif"           "output/CHELSA_bio16_1981-2010_V.2.1.tif"          
[17] "output/CHELSA_bio17_1981-2010_V.2.1.tif"           "output/CHELSA_bio18_1981-2010_V.2.1.tif"          
[19] "output/CHELSA_bio19_1981-2010_V.2.1.tif"           "output/CHELSA_cmi_mean_1981-2010_V.2.1.tif"       
[21] "output/CHELSA_hurs_mean_1981-2010_V.2.1.tif"       "output/CHELSA_npp_1981-2010_V.2.1.tif"            
[23] "output/CHELSA_pet_penman_mean_1981-2010_V.2.1.tif"

Then, I stack them

preds <- stack(preds)
names(preds)
 [1] "CHELSA_bio01_1981.2010_V.2.1"           "CHELSA_bio02_1981.2010_V.2.1"           "CHELSA_bio03_1981.2010_V.2.1"          
 [4] "CHELSA_bio04_1981.2010_V.2.1"           "CHELSA_bio05_1981.2010_V.2.1"           "CHELSA_bio06_1981.2010_V.2.1"          
 [7] "CHELSA_bio07_1981.2010_V.2.1"           "CHELSA_bio08_1981.2010_V.2.1"           "CHELSA_bio09_1981.2010_V.2.1"          
[10] "CHELSA_bio10_1981.2010_V.2.1"           "CHELSA_bio11_1981.2010_V.2.1"           "CHELSA_bio12_1981.2010_V.2.1"          
[13] "CHELSA_bio13_1981.2010_V.2.1"           "CHELSA_bio14_1981.2010_V.2.1"           "CHELSA_bio15_1981.2010_V.2.1"          
[16] "CHELSA_bio16_1981.2010_V.2.1"           "CHELSA_bio17_1981.2010_V.2.1"           "CHELSA_bio18_1981.2010_V.2.1"          
[19] "CHELSA_bio19_1981.2010_V.2.1"           "CHELSA_cmi_mean_1981.2010_V.2.1"        "CHELSA_hurs_mean_1981.2010_V.2.1"      
[22] "CHELSA_npp_1981.2010_V.2.1"             "CHELSA_pet_penman_mean_1981.2010_V.2.1"

Now I substitute the 1981.2010 with 1981-2010

preds2 <- gsub("1981.2010", "1981-2010", names(preds))
preds2
 [1] "CHELSA_bio01_1981-2010_V.2.1"           "CHELSA_bio02_1981-2010_V.2.1"           "CHELSA_bio03_1981-2010_V.2.1"          
 [4] "CHELSA_bio04_1981-2010_V.2.1"           "CHELSA_bio05_1981-2010_V.2.1"           "CHELSA_bio06_1981-2010_V.2.1"          
 [7] "CHELSA_bio07_1981-2010_V.2.1"           "CHELSA_bio08_1981-2010_V.2.1"           "CHELSA_bio09_1981-2010_V.2.1"          
[10] "CHELSA_bio10_1981-2010_V.2.1"           "CHELSA_bio11_1981-2010_V.2.1"           "CHELSA_bio12_1981-2010_V.2.1"          
[13] "CHELSA_bio13_1981-2010_V.2.1"           "CHELSA_bio14_1981-2010_V.2.1"           "CHELSA_bio15_1981-2010_V.2.1"          
[16] "CHELSA_bio16_1981-2010_V.2.1"           "CHELSA_bio17_1981-2010_V.2.1"           "CHELSA_bio18_1981-2010_V.2.1"          
[19] "CHELSA_bio19_1981-2010_V.2.1"           "CHELSA_cmi_mean_1981-2010_V.2.1"        "CHELSA_hurs_mean_1981-2010_V.2.1"      
[22] "CHELSA_npp_1981-2010_V.2.1"             "CHELSA_pet_penman_mean_1981-2010_V.2.1"

Finally, I assign the new name vector to preds, but the names remain wrong

names(preds) <- preds2
names(preds)
 [1] "CHELSA_bio01_1981.2010_V.2.1"           "CHELSA_bio02_1981.2010_V.2.1"           "CHELSA_bio03_1981.2010_V.2.1"          
 [4] "CHELSA_bio04_1981.2010_V.2.1"           "CHELSA_bio05_1981.2010_V.2.1"           "CHELSA_bio06_1981.2010_V.2.1"          
 [7] "CHELSA_bio07_1981.2010_V.2.1"           "CHELSA_bio08_1981.2010_V.2.1"           "CHELSA_bio09_1981.2010_V.2.1"          
[10] "CHELSA_bio10_1981.2010_V.2.1"           "CHELSA_bio11_1981.2010_V.2.1"           "CHELSA_bio12_1981.2010_V.2.1"          
[13] "CHELSA_bio13_1981.2010_V.2.1"           "CHELSA_bio14_1981.2010_V.2.1"           "CHELSA_bio15_1981.2010_V.2.1"          
[16] "CHELSA_bio16_1981.2010_V.2.1"           "CHELSA_bio17_1981.2010_V.2.1"           "CHELSA_bio18_1981.2010_V.2.1"          
[19] "CHELSA_bio19_1981.2010_V.2.1"           "CHELSA_cmi_mean_1981.2010_V.2.1"        "CHELSA_hurs_mean_1981.2010_V.2.1"      
[22] "CHELSA_npp_1981.2010_V.2.1"

What am I doing wrong here?

EDIT: Also, when I stack them, the hyphen changes to a point.

1 Answer 1

2

Raster, and by extension stack, names can't have minus signs in them.

> r = raster(matrix(1:12,3,4))
> names(r)
[1] "layer"
> names(r) = "the-layer"
> names(r)
[1] "the.layer"

Interestingly you can try setting the names element directly but even though the stored property has a minus sign, the printed value from the names call shows a dot:

> r@data@names = "the-layer"
> r@data@names
[1] "the-layer"
> names(r)
[1] "the.layer"

I can't see anything in ?raster::names that details limitations on the names of rasters, but I suspect the function validNames is involved.

help(raster::validNames)

Create valid names

Description:

     Create a set of valid names (trimmed, no duplicates, not starting
     with a number).

If you switch to the newer terra package, this restriction is lifted:

> tr = terra::rast(matrix(1:12,3,4))
> names(tr)
[1] "lyr.1"

Dots by default. Can we have minus signs?

> names(tr) = "lyr-1"
> names(tr)
[1] "lyr-1"

Yes!

1
  • Thanks! I didn't know about this restriction. Unfortunately, the package I'm using was written with raster, and not terra. But now I can easily work around this. Cheers
    – hiperhiper
    Commented Feb 18, 2023 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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