names.Rd
Get or set the names of the layers of a SpatRaster or the attributes of a SpatVector. With longnames
you can get or set the "long names" of a SpatRaster or SpatRasterDataset.
For a SpatRaster, you can also get/set a variable name or long name (one per data source).
See set.names
for in-place setting of names.
# S4 method for SpatRaster
names(x)
# S4 method for SpatRaster
names(x)<-value
# S4 method for SpatRaster
varnames(x)
# S4 method for SpatRaster
varnames(x)<-value
# S4 method for SpatRaster
longnames(x)
# S4 method for SpatRaster
longnames(x)<-value
# S4 method for SpatRasterDataset
names(x)
# S4 method for SpatRasterDataset
names(x)<-value
# S4 method for SpatRasterDataset
varnames(x)
# S4 method for SpatRasterDataset
varnames(x)<-value
# S4 method for SpatRasterDataset
longnames(x)
# S4 method for SpatRasterDataset
longnames(x)<-value
# S4 method for SpatVector
names(x)
# S4 method for SpatVector
names(x)<-value
SpatRaster, SpatRasterDataset, or SpatVector
character (vector)
character
terra enforces neither unique nor valid names. See make.unique
to create unique names and {make.names}
to make syntactically valid names.
s <- rast(ncols=5, nrows=5, nlyrs=3)
nlyr(s)
#> [1] 3
names(s)
#> [1] "lyr.1" "lyr.2" "lyr.3"
names(s) <- c("a", "b", "c")
names(s)
#> [1] "a" "b" "c"
# space is not valid
names(s)[2] <- "hello world"
names(s)
#> [1] "a" "hello world" "c"
# two invalid names
names(s) <- c("a", " a ", "3")
names(s)
#> [1] "a" " a " "3"
# SpatVector names
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
names(v)
#> [1] "ID_1" "NAME_1" "ID_2" "NAME_2" "AREA" "POP"
names(v) <- paste0(substr(names(v), 1, 2), "_", 1:ncol(v))
names(v)
#> [1] "ID_1" "NA_2" "ID_3" "NA_4" "AR_5" "PO_6"