# problem when doing algebra with raster layers in R

I'm trying to combine bioclim raster layers to create the following layer.

``````(Q) = AnnPrec/[(Tmax+Tmin)*(Tmax-Tmin)]*1000
``````

For that, I import a bioclim stack:

`````` envirc = stack("C:/Users/Agus/Dropbox/wiens SL paper/clime.grd")
``````

you can get the stack here: https://www.dropbox.com/sh/n8qfu9cmuowondp/AADcFHTnCqhhoYA43UeckVS-a?dl=0

separate specific layers

`````` AnnPrec=subset( envirc,"bio12")
Tmax=subset( envirc,"bio5")
Tmin=subset( envirc,"bio6")
``````

I could use overlay function , but this seems to work

`````` den=((Tmax+Tmin)*(Tmax-Tmin))*1000
``````

but now, this operation leads me to a constant value, which is incorrect, it # should be a range of values

`````` Q=(AnnPrec/den)
``````

using overlay function gives me the same value

`````` Q=overlay(AnnPrec,Tmax,Tmin, fun=function(x,y,z){return(x/((y+z)*(y-z))*1000)})
``````

What I'm doing wrong?

• Any chance of an example that doesn't require a 1GB download? Or at least `summary` on your layers so we can be sure your layers aren't constant values. I just tried on three random number rasters and I get a raster with some varying random numbers in. So my money is on a problem related to your data... – Spacedman Jan 29 '16 at 18:20
• You state that ` this operation leads me to a constant value` but that is not correct. Do `show(Q)` and you will see the range of values. Because of the odd range of values (mostly very close to zero, but with some outliers, the default legend is not helpful. But see `plot(log(Q))` or `plot(Q, zlim=c(-0.0001, 0.0001))` (and fdetsch's answer) – Robert Hijmans Jan 31 '16 at 19:57

Hm, I don't really see the problem. `overlay` works just fine on my machine. Here's the code.

``````# (Q)=AnnPrec/[(Tmax+Tmin)*(Tmax-Tmin)]*1000
Q <- overlay(AnnPrec, Tmax, Tmin, fun = function(x, y, z) {
x / ((y + z) * (y - z)) * 1000
}, filename = "q", overwrite = TRUE)
``````

And here's the resulting image including the range of values.

``````range(Q[], na.rm = TRUE)
 -11629.63  12039.74

spplot(Q, at = seq(-500, 500, 10),
colorkey = list(space = "top", width = .6),
scales = list(draw = TRUE))
`````` If that doesn't work, maybe you can provide the actual contents of `AnnPrec`, `Tmax` and `Tmin` as suggested by @Spacedman.