Reputation: 824
I have the following raster in R:
> veg
class : RasterLayer
dimensions : 22142, 18123, 401279466 (nrow, ncol, ncell)
resolution : 28.5, 28.5 (x, y)
extent : 329232, 845737.5, 8487420, 9118467 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=20 +south +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
data source : C:\Users\Desktop\RO SICAR\ibge_raster
names : ibge_raster
values : 1, 39 (min, max)
attributes :
ID COUNT NM_UVEG
from: 1 953 Contato Savana / Floresta Estacional
to : 39 57506 Savana Parque sem floresta-de-galeria
I am not so familiar with the raster package but I am trying to get the list of names ("NM_VEG") associated with factor levels ("ID") that R assigned to those names. I tried:
unique(values(veg))
But R returns the levels' "ID" instead of the names in "NM_UVEG":
> unique(values(veg))
[1] NA 5 8 4 14 34 2 13 12 28 36 26 11 25 10 16 17 33 38 3
[21] 15 9 23 29 27 32 22 31 37 6 39 35 7 1 20 24 30 19 18 21
I am sure I am missing some basic commands but I can't figure it out, any ideas? Thank you in advance!
Upvotes: 1
Views: 1018
Reputation: 162471
levels(veg)[[1]]
is the incantation you're looking for.
To show that that works, here (from here) is a some appropriate reproducible data ...
library(raster)
## Example data
r <- raster(ncol=4, nrow=2)
r[] <- sample(1:4, size=ncell(r), replace=TRUE)
r <- as.factor(r)
## Add a landcover column to the Raster Attribute Table
rat <- levels(r)[[1]]
rat[["landcover"]] <- c("land","ocean/lake", "rivers","water bodies")
levels(r) <- rat
... and here is the call that will extract a data.frame
that gives the attributes associated with each level:
levels(r)[[1]]
# ID landcover
# 1 1 land
# 2 2 ocean/lake
# 3 3 rivers
# 4 4 water bodies
Upvotes: 2