-1

I am using following code for quartile calculation in R using quantile function in raster package. My raster image stack has 307 images and all images have some No Data values. I just want the output without considering No Data values. I tried following code:

    library(raster)    
    raster_data<-list.files(path= getwd() , pattern= "\\.tif$", all.files=FALSE,
    full.names=TRUE,recursive=TRUE) 
    s <- stack(raster_data)
    quantile(s, na.rm = TRUE)

Output:

quantile(s)

                   0% 25% 50% 75% 100%

    X2008001h25v06 NA  NA  NA  NA   NA

    X2008002h25v06 NA  NA  NA  NA   NA

    X2008003h25v06 NA  NA  NA  NA   NA

    X2008004h25v06 NA  NA  NA  NA   NA

    X2008005h25v06 NA  NA  NA  NA   NA

    X2008006h25v06 NA  NA  NA  NA   NA
      --------------   
     -----------------  so on 

Why this NA NA NA NA NA values are coming in output instead of quartiles.

BetterCallMe
  • 636
  • 7
  • 15
  • It's hard to say without knowing what your data is or how it looks ... please add some more info and if possible make it [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Val Jul 05 '18 at 12:17

1 Answers1

0

Here is an example that works:

library(raster)
fn <- system.file("external/test.grd", package="raster")
s <- stack(fn, fn)
quantile(s)

#            0%      25%     50%      75%    100%
#test.1 128.434 293.2325 371.412 499.8195 1805.78
#test.2 128.434 293.2325 371.412 499.8195 1805.78

The same can be obtained (with smaller datasets) with:

t(apply(values(s), 2, quantile, na.rm=TRUE))

So you need to find out what is different about your files.

The only way I can think of that would return quantiles that are NA is when all values are NA:

quantile(rep(NA, 10), na.rm=TRUE)
# 0%  25%  50%  75% 100% 
# NA   NA   NA   NA   NA 

So it would seem that all values in your files are NA. That is perhaps not the case, but it is difficult to troubleshoot that without one of the files.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63