Apply.Rd 3.95 KB
Newer Older
Alasdair Hunter's avatar
Alasdair Hunter committed
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/Apply.R
\name{Apply}
\alias{Apply}
Alasdair Hunter's avatar
Alasdair Hunter committed
\title{Wrapper for Applying Atomic Functions to Arrays.}
Alasdair Hunter's avatar
Alasdair Hunter committed
\usage{
Apply(data, target_dims = NULL, fun, ..., output_dims = NULL,
  margins = NULL, guess_dim_names = TRUE, ncores = NULL,
  split_factor = 1)
Alasdair Hunter's avatar
Alasdair Hunter committed
}
\arguments{
\item{data}{A single object (vector, matrix or array) or a list of objects. They must be in the same order as expected by fun.}
Alasdair Hunter's avatar
Alasdair Hunter committed

\item{target_dims}{List of vectors containing the dimensions to be input into fun for each of the objects in the data. These vectors can contain either integers specifying the dimension position, or characters corresponding to the dimension names. This parameter is mandatory if margins is not specified. If both margins and target_dims are specified, margins takes priority over target_dims.}
Alasdair Hunter's avatar
Alasdair Hunter committed

\item{fun}{Function to be applied to the arrays.}
Alasdair Hunter's avatar
Alasdair Hunter committed

\item{...}{Additional arguments to be used in the fun.}
Alasdair Hunter's avatar
Alasdair Hunter committed

\item{output_dims}{Optional list of vectors containing the names of the dimensions to be output from the fun for each of the objects it returns (or a single vector if the function has only one output).}
Alasdair Hunter's avatar
Alasdair Hunter committed

\item{margins}{List of vectors containing the margins for the input objects to be split by. Or, if there is a single vector of margins specified and a list of objects in data, then the single set of margins is applied over all objects. These vectors can contain either integers specifying the dimension position, or characters corresponding to the dimension names. If both margins and target_dims are specified, margins takes priority over target_dims.}
Alasdair Hunter's avatar
Alasdair Hunter committed

\item{guess_dim_names}{Whether to automatically guess missing dimension names for dimensions of equal length across different inputs in 'data' with a warning (TRUE; default), or to crash whenever unnamed dimensions of equa length are identified across different inputs (FALSE).}

\item{ncores}{The number of multicore threads to use for parallel computation.}

\item{split_factor}{Factor telling to which degree the input data should be split into smaller pieces to be processed by the available cores. By default (split_factor = 1) the data is split into 4 pieces for each of the cores (as specified in ncores). A split_factor of 2 will result in 8 pieces for each of the cores, and so on. The special value 'greatest' will split the input data into as many pieces as possible.}
Alasdair Hunter's avatar
Alasdair Hunter committed
}
\value{
List of arrays or matrices or vectors resulting from applying fun to data.
Alasdair Hunter's avatar
Alasdair Hunter committed
}
\description{
This wrapper applies a given function, which takes N [multi-dimensional] arrays as inputs (which may have different numbers of dimensions and dimension lengths), and applies it to a list of N [multi-dimensional] arrays with at least as many dimensions as expected by the given function. The user can specify which dimensions of each array (or matrix) the function is to be applied over with the \code{margins} or \code{target_dims} option. A user can apply a function that receives (in addition to other helper parameters) 1 or more arrays as input, each with a different number of dimensions, and returns any number of multidimensional arrays. The target dimensions can be specified by their names. It is recommended to use this wrapper with multidimensional arrays with named dimensions.
Alasdair Hunter's avatar
Alasdair Hunter committed
}
\details{
When using a single object as input, Apply is almost identical to the apply function. For multiple input objects, the output array will have dimensions equal to the dimensions specified in 'margins'.
Alasdair Hunter's avatar
Alasdair Hunter committed
}
\examples{
Alasdair Hunter's avatar
Alasdair Hunter committed
#Change in the rate of exceedance for two arrays, with different 
#dimensions, for some matrix of exceedances.
data <- list(array(rnorm(1000), c(5, 10, 20)), 
             array(rnorm(500), c(5, 10, 10)), 
             array(rnorm(50), c(5, 10)))
test_fun <- function(x, y, z) {
  ((sum(x > z) / (length(x))) / 
  (sum(y > z) / (length(y)))) * 100
}
test <- Apply(data, target = list(3, 3, NULL), test_fun)
Alasdair Hunter's avatar
Alasdair Hunter committed
}
\references{
Wickham, H (2011), The Split-Apply-Combine Strategy for Data Analysis, Journal of Statistical Software.
}