Can't perform median() on a raster stack in R
Asked Answered
Q

1

5

I'm trying to find the per cell median across a set of rasters in R, but when I use the median function I get the following error:

Error in if (any(is.na(x))) return(x[FALSE][NA]) : 
  argument is not interpretable as logical

However, I can perform max, mean and calc(r, median) on my stack so I suspect that it isn't an issue with data format (tif). The code I'm using is:

Load data:

f = list.files("./")
r = stack(f)

Do sums:

median(r) # does not work
mean(r) # works
max(r) # works
calc(r, median) # works

As calc is working I don't need an answer to proceed, but I would like to understand why median isn't working. Is this standard behaviour?

Quite answered 24/8, 2015 at 13:59 Comment(2)
Because median is not a summary method available for Raster* objects. Available summary methods are currently mean, max, min, range, prod, sum, any and all. Source: reference manual, page 201Pollard
@Pascal consider putting this into an answer and don't forget to tell where this information is available.Salpiglossis
P
7

According to the reference manual, p. 201:

The following summary methods are available for Raster* objects: mean, max, min, range, prod, sum, any, all

median is not a summary method available for Raster* objects. That is why you need to use calc function.

Pollard answered 24/8, 2015 at 14:25 Comment(1)
median() is also absent from Spark R with Spark 1.6.2 - (This answer helped me realize that)Adah

© 2022 - 2024 — McMap. All rights reserved.