d <- c(4,16,32,32)
x <- array(rnorm(prod(d)), d)
z <- apply_time(x, function(v) {
y = matrix(NA, ncol=ncol(v), nrow=2)
y[1,] = (v[1,] + v[2,]) / 2
y[2,] = (v[3,] + v[4,]) / 2
y
})
dim(z)
[1] 2 16 32 32
Apply a function over pixel time series in a four-dimensional (band, time, y, x) array
Apply a function over pixel time series in a four-dimensional (band, time, y, x) array
Argument | Description |
---|---|
x | four-dimensional input array with dimensions band, time, y, x (in this order) |
FUN | function that receives a vector of band values in a one-dimensional array |
… | further arguments passed to FUN |
FUN is expected to produce a matrix (or vector if result has only one band) where rows are interpreted as new bands and columns represent time.
This is a helper function that uses the same dimension ordering as gdalcubes. It can be used to simplify the application of R functions e.g. over time series in a data cube.
# apply_time.array
Apply a function over pixel time series in a four-dimensional (band, time, y, x) array
```{r include=FALSE}
library(gdalcubes)
```
## Description
Apply a function over pixel time series in a four-dimensional (band, time, y, x) array
## Usage
```r
apply_time.array(x, FUN, ...)
```
## Arguments
| Argument | Description |
|:------------|:----------------------------------|
| x | four-dimensional input array with dimensions band, time, y, x (in this order) |
| FUN | function that receives a vector of band values in a one-dimensional array |
| ... | further arguments passed to FUN |
## Details
FUN is expected to produce a matrix (or vector if result has only one band) where rows are interpreted as new bands and columns represent time.
## Note
This is a helper function that uses the same dimension ordering as gdalcubes. It can be used to simplify
the application of R functions e.g. over time series in a data cube.
## Examples
```{r}
d <- c(4,16,32,32)
x <- array(rnorm(prod(d)), d)
z <- apply_time(x, function(v) {
y = matrix(NA, ncol=ncol(v), nrow=2)
y[1,] = (v[1,] + v[2,]) / 2
y[2,] = (v[3,] + v[4,]) / 2
y
})
dim(z)
```