可否raster
对象(在 R 中)具有不同模式(数据类型)的层?
从表面上看,我们似乎总是被迫选择一种类型:
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
有没有像 rasterDataFrame 这样的东西?
另外,请注意,我们大概不能使用 R 的因子,因为 raster@data 是一个矩阵,或者以其他方式强制转换为数字/整数。我错过了什么吗?
最佳答案
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
即将更新这使得上述解决方法变得不必要。
关于r - 光栅可以创建具有不同模式的多层对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16290208/