5

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

On the face of it it seems we are always forced to 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 anything like a rasterDataFrame?

Also, note that we presumably cannot use R's factors since the raster@data is a matrix, or otherwise coerced to numeric/integer. Am I missing something?

4

1 回答 1

3

raster包提供了使用分类变量创建栅格的能力,并且该rasterVis包包括用于绘制它们的函数。该ratify函数允许栅格包含一个查找表,将基础栅格整数值与其他值相关联,这些值可以是字符。这直接允许在批准的栅格的级别部分中使用任何其他模式的价值。

这是一个例子。

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)

即将进行的更新rasterVis使上述解决方法变得不必要。

于 2013-05-01T00:43:06.400 回答