我想使用包aggregate中的函数terra R来聚合栅格,并使用分位数方法作为聚合函数。下面,我使用quantilefrom 函数R base使用本地包目录中的栅格来计算第 50 个百分位(即中位数)。我选择第 50 个百分位数与中位数进行比较,但我的目标确实是计算其他分位数......
library(terra)
# load elevation coming with the terra pakage
r <- rast( system.file("ex/elev.tif", package="terra") )
plot(r)
# number of iteration
n_it <- 20
# with a custom function
start_time <- Sys.time()
for (i in 1:n_it){
ra <- aggregate(r, 2 , fun = function(x) quantile(x, probs = .5, na.rm = T))
}
end_time <- Sys.time()
Run Code Online (Sandbox Code Playgroud)
我的电脑大约花了 6秒做20次。
print(end_time-start_time)
Run Code Online (Sandbox Code Playgroud)
时差 6.052727 秒
aggregate当我使用中值内置函数运行相同的运行时,大约花费了 执行同样的 20 …