Can raster creation of multilayer objects with different modes?

Can a raster object (in R) have layers of different modes (data type)?

At first glance, it seems that we are always forced to use one type:

 library(raster) ## create a SpatialPixelsDataFrame with (trivially) two different "layer" types d <- data.frame(expand.grid(x = 1:10, y = 2:11), z = 1:100, a = sample(letters, 100, replace = TRUE), stringsAsFactors = FALSE) coordinates(d) <- 1:2 gridded(d) <- TRUE ## now coerce this to a raster brick or stack and our "a" is crushed to numeric NA all(is.na(getValues(brick(d)[[2]]))) [1] TRUE 

Is there something like a DataFrame bitmap file?

Also, note that we apparently cannot use R factors, since raster @ data are matrices or are otherwise forced to be numeric / integer. Did I miss something?

+4
source share
1 answer

The raster package provides the ability to create rasters with a categorical variable, while the rasterVis package includes functions for building them. The ratify function allows the ratify to include a lookup table that associates the basic values โ€‹โ€‹of the raster integer with other values, which can be characters. This directly allows you to use any other way of value in the levels of a rationalized raster.

Here is an example.

 library(rasterVis) r <- raster(xmn = 0, xmx = 1, ymn = 0, ymx = 2, nrow = 10, ncol = 11, crs = as.character(NA)) r[] <- sample(seq_along(letters[1:5]), ncell(r), replace = TRUE) ## ratify the raster, and set up the lookup table r <- ratify(r) rat <- levels(r)[[1]] rat$value <- letters[1:5] rat$code <- 1:5 ## workaround for limitation as at 2013-05-01 ## see https://stat.ethz.ch/pipermail/r-sig-geo/2013-May/018180.html rat$code <- NULL levels(r) <- rat levelplot(r) 

There are updates to rasterVis that make a workaround unnecessary.

+3
source

All Articles