Layer names changed after raster stack of blurred images

I have several raster layers which I applied a Gaussian filter to them. Then I stacked them but their names changed to focal_sum. What I want is to keep their original names (e.g., agbh020 instead of focal_sum).

Here is what I have tried so far:

library(raster)

setwd("/path")

l <- list.files(path = "path", 
                pattern="tif", 
                all.files = FALSE, 
                full.names = TRUE)

s = stack(l)

names(s) returns:

[1] "focal_sum.1" "focal_sum.2"

So far, I am changing manually theirs names using:

names(s) = c("agbh020", "agbh040", .....)

but you can understand that doing this for tens of layers it's time consuming and there is a big chance that I might make a mistake.

I tried solutions like this but I couldn't fix the problem.

Here are just two raster layers:

agbh020 = new("RasterLayer", file = new(".RasterFile", name = "C:\\Users\\Geography\\Desktop\\stack\\agbh020.tif", 
    datanotation = "FLT4S", byteorder = "little", nodatavalue = -Inf, 
    NAchanged = FALSE, nbands = 1L, bandorder = "BIL", offset = 0L, 
    toptobottom = TRUE, blockrows = c(rows = 17L), blockcols = c(cols = 119L), 
    driver = "gdal", open = FALSE), data = new(".SingleLayerData", 
    values = logical(0), offset = 0, gain = 1, inmemory = FALSE, 
    fromdisk = TRUE, isfactor = FALSE, attributes = list(), haveminmax = TRUE, 
    min = 0.0043362630531192, max = 11.062665939331, band = 1L, 
    unit = "", names = "focal_sum"), legend = new(".RasterLegend", 
    type = character(0), values = logical(0), color = logical(0), 
    names = logical(0), colortable = logical(0)), title = character(0), 
    extent = new("Extent", xmin = 6730900, xmax = 6778500, ymin = 3790500, 
        ymax = 3843300), rotated = FALSE, rotation = new(".Rotation", 
        geotrans = numeric(0), transfun = function () 
        NULL), ncols = 119L, nrows = 132L, crs = new("CRS", projargs = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"), 
    srs = "PROJCRS[\"unknown\",\n    BASEGEOGCRS[\"unknown\",\n        DATUM[\"World Geodetic System 1984\",\n            ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n                LENGTHUNIT[\"metre\",1]],\n            ID[\"EPSG\",6326]],\n        PRIMEM[\"Greenwich\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8901]]],\n    CONVERSION[\"unknown\",\n        METHOD[\"Mollweide\"],\n        PARAMETER[\"Longitude of natural origin\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8802]],\n        PARAMETER[\"False easting\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8806]],\n        PARAMETER[\"False northing\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8807]]],\n    CS[Cartesian,2],\n        AXIS[\"(E)\",east,\n            ORDER[1],\n            LENGTHUNIT[\"metre\",1,\n                ID[\"EPSG\",9001]]],\n        AXIS[\"(N)\",north,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1,\n                ID[\"EPSG\",9001]]]]", 
    history = list(), z = list())
agbh040 = new("RasterLayer", file = new(".RasterFile", name = "C:\\Users\\Geography\\Desktop\\stack\\agbh040.tif", 
    datanotation = "FLT4S", byteorder = "little", nodatavalue = -Inf, 
    NAchanged = FALSE, nbands = 1L, bandorder = "BIL", offset = 0L, 
    toptobottom = TRUE, blockrows = c(rows = 17L), blockcols = c(cols = 119L), 
    driver = "gdal", open = FALSE), data = new(".SingleLayerData", 
    values = logical(0), offset = 0, gain = 1, inmemory = FALSE, 
    fromdisk = TRUE, isfactor = FALSE, attributes = list(), haveminmax = TRUE, 
    min = 0.00014576742250938, max = 9.4912986755371, band = 1L, 
    unit = "", names = "focal_sum"), legend = new(".RasterLegend", 
    type = character(0), values = logical(0), color = logical(0), 
    names = logical(0), colortable = logical(0)), title = character(0), 
    extent = new("Extent", xmin = 6730900, xmax = 6778500, ymin = 3790500, 
        ymax = 3843300), rotated = FALSE, rotation = new(".Rotation", 
        geotrans = numeric(0), transfun = function () 
        NULL), ncols = 119L, nrows = 132L, crs = new("CRS", projargs = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"), 
    srs = "PROJCRS[\"unknown\",\n    BASEGEOGCRS[\"unknown\",\n        DATUM[\"World Geodetic System 1984\",\n            ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n                LENGTHUNIT[\"metre\",1]],\n            ID[\"EPSG\",6326]],\n        PRIMEM[\"Greenwich\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8901]]],\n    CONVERSION[\"unknown\",\n        METHOD[\"Mollweide\"],\n        PARAMETER[\"Longitude of natural origin\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8802]],\n        PARAMETER[\"False easting\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8806]],\n        PARAMETER[\"False northing\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8807]]],\n    CS[Cartesian,2],\n        AXIS[\"(E)\",east,\n            ORDER[1],\n            LENGTHUNIT[\"metre\",1,\n                ID[\"EPSG\",9001]]],\n        AXIS[\"(N)\",north,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1,\n                ID[\"EPSG\",9001]]]]", 
    history = list(), z = list())

I found this post, which utilizes the tools package, so you can do something like this (insted of the raster package, I used terra for the raster stack):

l <- list.files(path = "path", 
                pattern = "tif", 
                all.files = FALSE, 
                full.names = FALSE)



s = rast(l)
names(s) = tools::file_path_sans_ext(l)
names(s)

Which returns:

1 "agbh020" "agbh040"

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.