Call a multi-argument function with values taken from columns of an data frame or array, and combine results into an array

maply(.data, .fun = NULL, ..., .expand = TRUE, .progress = "none",
  .inform = FALSE, .drop = TRUE, .parallel = FALSE, .paropts = NULL)

Arguments

.data

matrix or data frame to use as source of arguments

.fun

function to apply to each piece

...

other arguments passed on to .fun

.expand

should output be 1d (expand = FALSE), with an element for each row; or nd (expand = TRUE), with a dimension for each variable.

.progress

name of the progress bar to use, see create_progress_bar

.inform

produce informative error messages? This is turned off by default because it substantially slows processing speed, but is very useful for debugging

.drop

should extra dimensions of length 1 in the output be dropped, simplifying the output. Defaults to TRUE

.parallel

if TRUE, apply function in parallel, using parallel backend provided by foreach

.paropts

a list of additional options passed into the foreach function when parallel computation is enabled. This is important if (for example) your code relies on external data or packages: use the .export and .packages arguments to supply them so that all cluster nodes have the correct environment set up for computing.

Value

if results are atomic with same type and dimensionality, a vector, matrix or array; otherwise, a list-array (a list with dimensions)

Details

The m*ply functions are the plyr version of mapply, specialised according to the type of output they produce. These functions are just a convenient wrapper around a*ply with margins = 1 and .fun wrapped in splat.

Input

Call a multi-argument function with values taken from columns of an data frame or array

Output

If there are no results, then this function will return a vector of length 0 (vector()).

References

Hadley Wickham (2011). The Split-Apply-Combine Strategy for Data Analysis. Journal of Statistical Software, 40(1), 1-29. http://www.jstatsoft.org/v40/i01/.

See also

Other array output: aaply, daply, laply

Other multiple arguments input: m_ply, mdply, mlply

Examples

maply(cbind(mean = 1:5, sd = 1:5), rnorm, n = 5)
#> , , = 1 #> #> sd #> mean 1 2 3 4 5 #> 1 1.979957 NA NA NA NA #> 2 NA 5.065483 NA NA NA #> 3 NA NA 6.157616 NA NA #> 4 NA NA NA 7.217646 NA #> 5 NA NA NA NA -3.952959 #> #> , , = 2 #> #> sd #> mean 1 2 3 4 5 #> 1 2.321781 NA NA NA NA #> 2 NA 2.858295 NA NA NA #> 3 NA NA 5.033051 NA NA #> 4 NA NA NA -3.600243 NA #> 5 NA NA NA NA 1.058706 #> #> , , = 3 #> #> sd #> mean 1 2 3 4 5 #> 1 -0.1197108 NA NA NA NA #> 2 NA 2.244207 NA NA NA #> 3 NA NA 3.115499 NA NA #> 4 NA NA NA 7.743137 NA #> 5 NA NA NA NA -0.6651083 #> #> , , = 4 #> #> sd #> mean 1 2 3 4 5 #> 1 1.5146 NA NA NA NA #> 2 NA -0.2760248 NA NA NA #> 3 NA NA 1.930856 NA NA #> 4 NA NA NA 2.763794 NA #> 5 NA NA NA NA 6.818263 #> #> , , = 5 #> #> sd #> mean 1 2 3 4 5 #> 1 -0.5090998 NA NA NA NA #> 2 NA 0.8839697 NA NA NA #> 3 NA NA 5.348532 NA NA #> 4 NA NA NA 5.052267 NA #> 5 NA NA NA NA 3.57056 #>
maply(expand.grid(mean = 1:5, sd = 1:5), rnorm, n = 5)
#> , , = 1 #> #> sd #> mean 1 2 3 4 5 #> 1 1.517669 4.527579 3.82361837 -4.725083 2.199798 #> 2 2.768721 3.224182 -0.99529765 2.515692 2.039431 #> 3 5.211769 -1.893360 -1.58687614 8.823366 1.458943 #> 4 4.554186 9.297864 4.21316008 9.840441 5.833372 #> 5 6.142695 6.084283 -0.05951423 1.374639 3.081335 #> #> , , = 2 #> #> sd #> mean 1 2 3 4 5 #> 1 0.8970913 1.265984 -0.01680762 6.5316434 1.304494 #> 2 3.0359308 1.141240 5.70017019 -1.3208570 11.388719 #> 3 1.9583316 3.130973 3.71227604 -0.3144141 8.060009 #> 4 5.9931103 1.693203 2.08139569 4.5987174 9.649176 #> 5 6.1238388 5.150212 2.29155515 -3.4086099 4.129568 #> #> , , = 3 #> #> sd #> mean 1 2 3 4 5 #> 1 0.02593041 1.7529987 0.7732773 1.01250376 -9.8878801 #> 2 1.52611293 4.7209227 3.0212735 -0.01437164 12.7937828 #> 3 1.85347615 0.8029822 -0.9384427 4.15909784 -1.5952580 #> 4 3.84587926 3.3187242 1.4644128 -1.73328440 -0.7074904 #> 5 4.60299851 6.1170288 8.9529011 12.57344185 3.8912774 #> #> , , = 4 #> #> sd #> mean 1 2 3 4 5 #> 1 2.2706723 3.277415 1.1206132 0.6884527 0.41069928 #> 2 0.7246651 1.858285 0.5818926 -2.7745647 5.54857261 #> 3 1.3246727 1.733644 5.2410858 1.0797861 5.81690039 #> 4 6.5644083 5.572725 6.0257341 3.9587867 5.08918821 #> 5 4.1767388 5.830813 8.3005692 1.1274967 -0.04764361 #> #> , , = 5 #> #> sd #> mean 1 2 3 4 5 #> 1 1.960865 3.482526 1.372903 2.7657129 1.561474 #> 2 1.694379 1.455693 4.126259 -1.0068933 5.834917 #> 3 4.525939 -1.127309 -1.687555 0.5806826 4.612414 #> 4 5.061999 1.458974 7.460127 3.1510559 11.077061 #> 5 4.421115 2.095400 8.611304 4.5895879 7.403626 #>
maply(cbind(1:5, 1:5), rnorm, n = 5)
#> , , = 1 #> #> #> 1 2 3 4 5 #> 1 2.604407 NA NA NA NA #> 2 NA 6.224555 NA NA NA #> 3 NA NA 3.595176 NA NA #> 4 NA NA NA -2.354482 NA #> 5 NA NA NA NA 6.96672 #> #> , , = 2 #> #> #> 1 2 3 4 5 #> 1 -0.5150245 NA NA NA NA #> 2 NA 1.287751 NA NA NA #> 3 NA NA 1.798784 NA NA #> 4 NA NA NA 1.840307 NA #> 5 NA NA NA NA 5.210671 #> #> , , = 3 #> #> #> 1 2 3 4 5 #> 1 -0.4160239 NA NA NA NA #> 2 NA -0.1289284 NA NA NA #> 3 NA NA 4.848463 NA NA #> 4 NA NA NA -0.6778459 NA #> 5 NA NA NA NA 10.89832 #> #> , , = 4 #> #> #> 1 2 3 4 5 #> 1 1.876777 NA NA NA NA #> 2 NA 4.154233 NA NA NA #> 3 NA NA 8.92247 NA NA #> 4 NA NA NA 6.236424 NA #> 5 NA NA NA NA 3.715394 #> #> , , = 5 #> #> #> 1 2 3 4 5 #> 1 1.624132 NA NA NA NA #> 2 NA 4.363151 NA NA NA #> 3 NA NA 8.653987 NA NA #> 4 NA NA NA -3.277389 NA #> 5 NA NA NA NA -0.2816805 #>