[R-sig-Geo] Error in applying calc to rasterbrick with NA vallues

Loïc Dutrieux |o|c@dutr|eux @end|ng |rom c|r@d@|r
Fri May 8 17:08:28 CEST 2020


Hi Jackson,

All the layers of the toy dataset you create in your example are 
identical. Is that intentional?
Below I create a more heterogeneous dataset and introduce a few NAs.

library(raster)
library(trend)

set.seed(12)
r <- raster(nrow=10, ncol=10)
s <- lapply(1:200, function(i) setValues(r, rnorm(ncell(r), 
sample.int(5,1), 0.5)))
s <- stack(s)

s[s < 0] <- NA

# Visualize that some pixels have NAs and other don't
hasna <- stackApply(s, indices = 1, fun = function(x, na.rm){anyNA(x)})
plot(hasna)

# pettitt.test function does not handle NAs, so you need to add a condition
# to "exclude" vectors that contain NAs
fun <- function(x){
     out <- ifelse(anyNA(x),
                   yes = NA,
                   no = unname(pettitt.test(x)[3]$estimate))
     return(out)
}

# Check that it works
fun(as.vector(s[1])) # first pixel, does not contain NAs
fun(as.vector(s[2])) # second pixel, contains NAs

# Check that it works with calc
s_out <- calc(s, fun = fun)
plot(s_out)


Cheers,
Loïc

On 05/08/2020 03:59 PM, Jackson Rodrigues wrote:
> Dear all,
> 
> I am sorry for previous email. I am learning how to handle correctly this
> list.
> 
> On R-sig-Geo Digest, Vol 201, Issue 5  I got a smart solution for my tests
> with parallelization.
> 
> However, my original data has NA values, and applying the function created
> to my data I get some errors.
> I have played around with other commands like na.rm, na.action=na.exclude
> or reclassifications and none worked.
> 
> Could someone give me some herp again?
> 
> best wishes,
> 
> Jackson
> ################
> library(raster)
> library(trend)
> 
> r <- raster(ncol=96, nrow=63, vals=runif(63*96))
> n <- 672  # number of copies
> s <- stack(lapply(1:n, function(i) rz<-stack(r[i]<-(r^2/3))))
> s[s < 0.1] <- NA
> 
> # Define the function to apply to each pixel; it should return a numeric
> or a vector of numerics with always the same lenght
> fun <- function(x){
>    out <- pettitt.test(x)[3]$estimate
>    return(unname(out))
> }
> 
> # Check that it works
> fun(seq(2000))
> 
> # Check that it works with calc
> s_out <- calc(s, fun = fun)
> 
> Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
>    cannot use this function
> 
> # Parallelize with clusterR
> beginCluster()
> s_out <- clusterR(s, fun=calc, args=list(fun=fun))
> 
> [1] "cannot use this function"
> attr(,"class")
> [1] "snow-try-error" "try-error"
> Error in clusterR(s, fun = calc, args = list(fun = fun)) : cluster error
> 
> endCluster()
> ##############
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 



More information about the R-sig-Geo mailing list