我有一个时间序列的光栅图像,使用R中的光栅包作为光栅堆栈,其中每个栅格的计算值范围为0 - 100.但是,当有云覆盖时,这些像素编码为255.我想要计算堆积栅格的平均值但在平均值的计算中排除那些像素值,即255
我正在使用的代码如下,非常感谢任何指导。
exports.testUnsub = functions.https.onRequest((req, res) => {
var db = admin.database();
var ref = db.ref("mailingList/-KhBOisltrOmv57Mrzus");
ref.child("subscribed").set(false);
console.log("-KhBOisltrOmv57Mrzus has unsubscribed from mailing list.");
});
答案 0 :(得分:1)
如果您可以添加可重现的示例,将会很有帮助。根据您的解释,您的数据可能如下所示:
# create a variable that countains 98 values between 0 - 100 and 2 times 255.
test = c(runif(49,min = 0, max = 100),255,runif(49,min = 0, max = 100),255)
# calculating the mean without the 255 values
mean(test[-which(test==255)])
答案 1 :(得分:1)
您可以将所有255个值指定为NA
。通过稍后加入na.rm = TRUE
,这些值将从平均值中排除。
set.seed(4)
r <- raster(matrix(sample(1:255, 100, replace = T), ncol = 20))
r <- stack(r, r)
summary(getValues(r))
# layer.1 layer.2
# Min. : 2.0 Min. : 2.0
# 1st Qu.: 72.5 1st Qu.: 72.5
# Median :145.0 Median :145.0
# Mean :140.2 Mean :140.2
# 3rd Qu.:209.5 3rd Qu.:209.5
# Max. :255.0 Max. :255.0
r[r==255] <- NA
summary(getValues(r))
# layer.1 layer.2
# Min. : 2.0 Min. : 2.0
# 1st Qu.: 72.0 1st Qu.: 72.0
# Median :145.0 Median :145.0
# Mean :139.1 Mean :139.1
# 3rd Qu.:208.5 3rd Qu.:208.5
# Max. :253.0 Max. :253.0 # <- Highest number is not 255 anymore
# NA's :1 NA's :1 # <- raster includes NA's now (where 255 has been)
之后,raster::cellsStats()
为每个图层创建一个平均值。
cellStats(r, mean, na.rm = TRUE)
# layer.1 layer.2
# 139.0606 139.0606
如果要在RasterStack r
中返回具有所有栅格图层平均值的RasterLayer,只需在用NA
替换所有255个值后写入:
mean(r)