# NOT RUN {
set.seed(0)
r <- raster(nrow=10, ncol=10)
values(r) <- runif(ncell(r)) * 10
is.factor(r)
r <- round(r)
f <- as.factor(r)
is.factor(f)
x <- levels(f)[[1]]
x
x$code <- letters[10:20]
levels(f) <- x
levels(f)
f
r <- raster(nrow=10, ncol=10)
values(r) = 1
r[51:100] = 2
r[3:6, 1:5] = 3
r <- ratify(r)
rat <- levels(r)[[1]]
rat$landcover <- c('Pine', 'Oak', 'Meadow')
rat$code <- c(12,25,30)
levels(r) <- rat
r
# extract values for some cells
i <- extract(r, c(1,2, 25,100))
i
# get the attribute values for these cells
factorValues(r, i)
# write to file:
# rr <- writeRaster(r, rasterTmpFile(), overwrite=TRUE)
# rr
# create a single-layer factor
x <- deratify(r, 'landcover')
x
is.factor(x)
levels(x)
# }
Run the code above in your browser using DataLab