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.