diff --git a/README.md b/README.md index a6cfa357043700e60f636fe03485f60a44609ccd..545082480370a520e8cd3b1c52118771300fe1a3 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,51 @@ ---- -title: "Apply a Function Taking Multiple Numeric Objects as Input Across Multiple Arrays" -author: "Alasdair" -date: "14 July 2017" -output: html_document ---- +## multiApply -This package extends the apply and plyr families of functions to applications which involve the use of multiple arrays as input. +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. -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 write loops for every application. +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. +### Installation +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 +```