0
Follow
2
View

aggregate raster with custom function fails

ppatest 注册会员
2023-01-25 12:44

Example data

library(terra)
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)

You can compute the percentage of cells that are not NA with a simple aggregate function

a <- aggregate(!is.na(r), 2, mean) * 100 

Or, more efficiently

a <- aggregate(is.finite(r), 2, mean) * 100 

But this also seem to work fine with your function

nofun <- function(x){ 100 * length(na.omit(x)) / length(x)}
b <- aggregate(r, 2, nofun)
plot(b, plg=list(title="NA (%)"))

By adding na.rm=TRUE you provide an additional argument to length that it does not have. It only has one argument x.

About the Author

Question Info

Publish Time
2023-01-25 12:44
Update Time
2023-01-25 12:44