如何使用类别名称对 terra 中的分类栅格进行重新分类
我希望能够合并分类栅格中的两个类别。到目前为止我找到的唯一解决方案使用级别索引号,而不是类别名称。我如何使用类别名称来做到这一点?
library(terra)
m <- matrix(rep(c("a", "b", "c"), each = 3), nrow = 3, ncol = 3)
x <- rast(m)
x[x$lyr.1 == "c"]
m2 <- matrix(c("a", "a", "b", "b", "c", "b"), nrow = 3, ncol = 2, byrow = TRUE)
test <- classify(x, m2)
#doesn't work with category names
test <- subst(x, "c", "b")
#doesn't work with category names
test <- subst(x, 2, 1)
#works with category index
I'd like to be able to merge two categories in a categorical raster. The only solution I've figured out so far uses the level index number, not the name of the category. How could I do this using the name of the category?
library(terra)
m <- matrix(rep(c("a", "b", "c"), each = 3), nrow = 3, ncol = 3)
x <- rast(m)
x[x$lyr.1 == "c"]
m2 <- matrix(c("a", "a", "b", "b", "c", "b"), nrow = 3, ncol = 2, byrow = TRUE)
test <- classify(x, m2)
#doesn't work with category names
test <- subst(x, "c", "b")
#doesn't work with category names
test <- subst(x, 2, 1)
#works with category index
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
示例数据
对于当前版本的 terra,您可以执行以下任一
操作:
Example data
With the current version of terra you can do either
or
图(x)
plot(x)