我正在研究R中的土地利用/覆盖变化模拟。我有两个光栅地图,一个是“土地利用/覆盖类”,另一个是“森林砍伐风险信息”。我正在使用风险栅格来识别更可能被砍伐的森林像素(其中一个土地利用/覆盖类别)。到目前为止,我有一个可用的R代码,这是一个可以复制的例子:
#create land-use/cover raster
real <- raster(nrows=25, ncols=25, vals=round(rnorm(625, 3), 0))
real[ real > 3 ] <- 3 #just so we end with 3 land-use/cover classes
real[ real < 1 ] <- 1 #just so we end with 3 land-use/cover classes
plot(real)
#function to create the deforestation risk raster created by someone else
rtnorm <- function(n, mean = 0, sd = 1, min = 0, max = 1) {
bounds <- pnorm(c(min, max), mean, sd)
u <- runif(n, bounds[1], bounds[2])
qnorm(u, mean, sd)
}
risk <- raster(nrows=25, ncols=25, vals=rtnorm(n = 625, .1, .9)) #deforestation risk raster
plot(risk)
#The actual analysis starts here:
forest <- real #read the land-use/cover raster
forest[ forest != 3 ] <- NA #all left is class 3 (let's say, forest) in the raster
plot(forest, col="green")
deforestation <- sum(forest, risk) #identify the forest pixels with highest risk
plot(deforestation)
deforestation[ deforestation <= 3.5 ] <- 0 #rule to deforest the forest pixels
deforestation[ deforestation > 0 ] <- 100 #mark the pixels that will be deforested
plot(deforestation)
simulation <- sum(real, deforestation)
simulation[ simulation > 100 ] <- 2 #I use this to mark the forest pixels to a different land-use/cover class
plot(simulation)
我想更改我用来选择将被砍伐的森林像素的规则(即deforestation[ deforestation <= 3.5 ] <- 0
)。而不是选择像3.5
这样的阈值,我想知道是否可以设置特定数量的森林像素(例如50)进行砍伐,然后选择具有最高森林砍伐风险的50个森林像素。
我完全不知道如何在R中做这样的事情,所以任何建议都将受到高度赞赏。谢谢。
答案 0 :(得分:1)
如果栅格不是太大,您可以获得具有最高值的50个单元格,如下所示:
i <- order(values(deforestation), decreasing=TRUE)[1:50]
然后您可以像这样使用这些
deforestation[] <- 0
deforestation[i] <- 100