I'm doing a pretty basic aggregation operation using aggregate
from the terra
package. The main idea is to calculate the percentage of pixels with values over the whole number using the following function:
nofun = function(x){ length(na.omit(x))/length(x) * 100 }
Unfortunately, aggregate
fails under different conditions -even simpler- and I can't figure out what I'm doing wrong.
First try: aggregate(chm, fact=20, fun=length, na.rm=T) # w/o na.rm=T
Error in FUN(X[[i]], ...) : 2 arguments passed to 'length' which requires 1
Second try:
aggregate(chm, fact=20, fun=function(x){ length(x) } )
Error: [aggregate] this function does not return the correct number of values
Same result applying the above-mentioned final function modified according to this reply, as follows:
function(x){ if(any(is.numeric(x))){length(na.omit(x))/length(x) * 100} else {NA_real_}}
Everything tested both in terra
1.4.22
and 1.5.12
on W10.