Reputation: 87
I have a raster matrix with non-negative values (each pixel has a height value), and would like to calculate some custom height metrics with a 5x5 moving window, using symmetrical padding around the edges of the matrix .
I had done some basic calculations using pktools (spatial filter) in QGIS (mean, standard deviation, maximum), which allowed me to do this (padding using zeros, replicates or symmetrical).
Now that I want to calculate some other metrics using custom functions (i.e. coefficient of heights, skewness and kurtosis), I cannot use pktools. I have been trying to use the focal()
function in raster
, but the padValue needs to be numeric. Is there a way around this, so I can have symmetrical borders? I tried the following:
> f.CoH <- function(x) {sd(x)/mean(x)}
>
> CoH <- focal(raster, w=matrix(1,nrow=5,ncol=5), fun=f.CoH, pad=TRUE,
> padValue="symmetric")
But get the following error:
> Warning message: In .local(x, ...) : NAs introduced by coercion
Thank you
Upvotes: 0
Views: 242
Reputation: 5932
You could try padValue = NA
.
You'd also need to modify the function to deal with NAs:
f.CoH <- function(x) {sd(x, na.rm = TRUE)/mean(x, na.rm = TRUE)}
Consider however that this would give somewhat different results wrt "symmetrical padding", because the standard deviation and average obtained by padding with NAs and by replicating values would be different.
HTH
Upvotes: 1