# lapp: Apply a function to layers of a SpatRaster, or sub-datasets... In terra: Spatial Data Analysis

## Description

Apply a function to a SpatRaster, using layers as arguments.

The number of arguments in function `fun` must match the number of layers in the SpatRaster (or the number of sub-datasets in the SpatRasterDataset). For example, if you want to multiply two layers, you could use this function: `fun=function(x,y){return(x*y)}` percentage: `fun=function(x,y){return(100 * x / y)}`. If you combine three layers you could use `fun=function(x,y,z){return((x + y) * z)}`

Before you use the function, test it to make sure that it is vectorized. That is, it should work for vectors longer than one, not only for single numbers. The function must return the same number of elements as its input vectors, or multiples of that. Also make sure that the function is NA-proof: it should returns the same number of values when some or all input values are `NA`. And the function must return a vector or a matrix, not a `data.frame`.

Use `app` for summarize functions such as `sum`, that take any number of arguments; and `tapp` to do so for groups of layers.

## Usage

 ```1 2 3 4 5``` ```## S4 method for signature 'SpatRaster' lapp(x, fun, ..., usenames=FALSE, filename="", overwrite=FALSE, wopt=list()) ## S4 method for signature 'SpatRasterDataset' lapp(x, fun, ..., recycle=FALSE, filename="", overwrite=FALSE, wopt=list()) ```

## Arguments

 `x` SpatRaster or SpatRasterDataset `fun` a function that takes a vector and can be applied to each cell of `x` `...` additional arguments to be passed to `fun` `usenames` logical. Use the layer names to match the function arguments? If `FALSE` matching is by position `recycle` logical. Recycle layers to match the subdataset with the largest number of layers `filename` character. Output filename `overwrite` logical. If `TRUE`, `filename` is overwritten `wopt` list with named options for writing files as in `writeRaster`

SpatRaster

## Note

Use `sapp` or `lapply` to apply a function that takes a SpatRaster as argument to each layer of a SpatRaster (that is rarely necessary).

` app, tapp, math`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35``` ```s <- rast(system.file("ex/logo.tif", package="terra")) + 1 ss <- s[[2:1]] fvi <- function(x, y){ (x - y ) / (x + y) } x <- lapp(ss, fun=fvi ) # which is the same as supplying the layers to "fun" # in some cases this will be much faster y <- fvi(s[], s[]) f2 <- function(x, y, z){ (z - y + 1) / (x + y + 1) } p1 <- lapp(s, fun=f2 ) p2 <- lapp(s[[1:2]], f2, z=200) # the usenames argument fvi2 <- function(red, green){ (red - green ) / (red + green) } names(s) x1 <- lapp(s[[1:2]], fvi2, usenames=TRUE) x2 <- lapp(s[[2:1]], fvi2, usenames=TRUE) # x1 and x2 are the same, despite the change in the order of the layers # x4 is also the same, but x3 is not x3 <- lapp(s[[2:1]], fvi2, usenames=FALSE) x4 <- lapp(s, fvi2, usenames=TRUE) # while this would fail because # there are too many layers in s # x5 <- lapp(s, fvi2, usenames=FALSE) pairs(c(x1, x2, x3, x4)) ## SpatRasterDataset x <- sds(s, s[]+50) lapp(x, function(x, y) x/y, recycle=TRUE) ```

### Example output ```terra version 0.9.8
 "red"   "green" "blue"
class       : SpatRaster
dimensions  : 77, 101, 3  (nrow, ncol, nlyr)
resolution  : 1, 1  (x, y)
extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs
data source : memory
names       :        red,      green,       blue
min values  : 0.01960784, 0.01851852, 0.01694915
max values  :  0.8366013,  0.8857143,  1.1235955
```

terra documentation built on Aug. 20, 2021, 9:07 a.m.