Search code examples
rraster

How to extract raster layer names from raster stack R?


I have a raster stack that I created from a geotiff:

ras = "C:/temp/subset.tif"
b1 = raster(ras, band = 1)
b2 = raster(ras, band = 2)
b3 = raster(ras, band = 3)

sf = stack(b1, b2, b3)

Printing the stack yields the following information:

> print(sf)
class       : RasterStack 
dimensions  : 701, 1151, 806851, 3  (nrow, ncol, ncell, nlayers)
resolution  : 1, 1  (x, y)
extent      : 301259.5, 302410.5, 4694849, 4695550  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=13 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0 
names       : subset.1, subset.2, subset.3 
min values  :       51,       64,       65 
max values  :      229,      224,      218

How can I extract a single layer name from the stack? For example:

'subset.1'

Solution

  • Use the names function to describe the raster stack names. For example:

    > names(sf)
    [1] "subset.1" "subset.2" "subset.3"
    
    > names(sf)[1]
    [1] "subset.1"