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

Jackson Rodrigues j@ck@onmrodr|gue@ @end|ng |rom gm@||@com
Sat May 9 12:36:29 CEST 2020


Hi Loic,

Thank you again.

Actually, I did not worry about data quality created.
My concern was a creation of the same amount of data I originally have.

Your suggestion worked perfectly! I tried to replace NAs by 0 in the
begining, but I got errors.

So, thank you again.

Cheers

Jackson

Em sex, 8 de mai de 2020 12:08, Loïc Dutrieux <loic.dutrieux using cirad.fr>
escreveu:

> 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
> >
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list