README.md 2.93 KB
Newer Older
## multiApply [![build status](https://earth.bsc.es/gitlab/ces/multiApply/badges/master/build.svg)](https://earth.bsc.es/gitlab/ces/multiApply/commits/master) [![CRAN version](http://www.r-pkg.org/badges/version/multiApply)](https://cran.r-project.org/package=multiApply) [![coverage report](https://earth.bsc.es/gitlab/ces/multiApply/badges/master/coverage.svg)](https://earth.bsc.es/gitlab/ces/multiApply/commits/master) [![dependencies](https://tinyverse.netlify.com/badge/multiApply)](https://cran.r-project.org/package=multiApply) [![License: LGPL v3](https://img.shields.io/badge/License-LGPL%20v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0) [![CRAN RStudio Downloads](https://cranlogs.r-pkg.org/badges/multiApply)](https://cran.rstudio.com/web/packages/multiApply/index.html)
Nicolau Manubens's avatar
Nicolau Manubens committed
This package extends the apply and plyr families of functions to applications which involve the use of multiple arrays as input, and is useful to apply a function taking multiple numeric objects as input across multiple multi-dimensional arrays.
Alasdair Hunter's avatar
Alasdair Hunter committed

Nicolau Manubens's avatar
Nicolau Manubens committed
This is especially useful for climate data related applications, where data is often distributed across multiple arrays with different dimensions (e.g experimental array 1, experimental array 2 and the observations). The `multiApply::Apply` function reduces the need to write loops for every application.
Alasdair Hunter's avatar
Alasdair Hunter committed

Nicolau Manubens's avatar
Nicolau Manubens committed
### Installation
Alasdair Hunter's avatar
Alasdair Hunter committed

Nicolau Manubens's avatar
Nicolau Manubens committed
In order to install and load the latest published version of the package on CRAN, you can run the following lines in your R session:

```r
install.packages('multiApply')
library(multiApply)
```

Also, you can install the latest stable version from this GitHub repository as follows:

```r
devtools::install_git('https://earth.bsc.es/gitlab/ces/multiApply')
```

### How to use

This package consistis in a single function, `Apply`, which is used in a similar fashion as the base `apply`. Full documentation can be found in `?Apply`.

A simple example is provided next. In this example, we have two data arrays. The first, with information on the number of items sold in 5 different stores (located in different countries) during the past 1000 days, for 200 different items. The second, with information on the price point for each item in each store.

The example shows how to compute the total income for each of the stores, straightforwardly combining the input data arrays, by automatically applying repeatedly the 'atomic' function that performs only the essential calculations for a single case.

```r
dims <- c(store = 5, item = 200, day = 1000)
sales_amount <- array(rnorm(prod(dims)), dims)

dims <- c(store = 5, item = 200)
sales_price <- array(rnorm(prod(dims)), dims)

income_function <- function(x, y) {
  # Expected inputs:
  #  x: array with dimensions (item, day)
  #  y: price point vector with dimension (item)
  sum(rowSums(x) * y)
}

income <- Apply(data = list(sales_amount, sales_price),
                target_dims = list(c('item', 'day'), 'item'), 
                income_function)

dim(income[[1]])
# store
#     5
```