# Using erfc function in r raster package, to convert temperature data to permafrost probability?

At present, I am looking at mapping permafrost in mountains, which is modelled as a function of Mean Annaul Air Temperature. To predict statistically the probability of permafrost, you use an equation with the erfc function to predict the probablity of permafrost occuring at any given point (0-1). So far I am having no such luck with this. Converting one number through erfc is simple in Excel but I cannot seem to do it for an entire raster dataset in R, whereas this is simple to do when using simple mathematical commands (adding, multiplying etc), for the whole dataset.

Any tips?

You can update any raster with any function in R by doing:

``````r[] = anything(r[])
``````

as long as the `anything` function takes a vector and returns a vector of the same length.

The error function is (I think):

``````erfc = function(x){2 * pnorm(x * sqrt(2), lower.tail = FALSE)}
``````

Make a test 3x4 raster with 0 to 1:

``````> r = raster(matrix(seq(0,1,len=12),3,4))
``````

Calling the function fails:

``````> erfc(r)
Error in pnorm(x * sqrt(2), lower.tail = FALSE) :
Non-numeric argument to mathematical function
``````

But replacing the values like this works:

``````> r[] = erfc(r[])
> r[]
 1.0000000 0.6997229 0.4404763 0.2472381 0.8977020 0.6070706 0.3681447
 0.1985657 0.7970786 0.5203381 0.3037058 0.1572992
``````

If you want to keep the original raster, make a copy, or you can use `calc` which returns a new raster:

``````> r2 = calc(r,erfc)
> r2[]
 0.1572992 0.3223904 0.5333322 0.7266033 0.2042477 0.3906013 0.6026209
 0.7788528 0.2596413 0.4618103 0.6675559 0.8239600
>
``````