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. https://www.jstatsoft.org/v40/i01/.

See also

Other multiple arguments input: m_ply(), mdply(), mlply()

Other array output: aaply(), daply(), laply()

Examples

maply(cbind(mean = 1:5, sd = 1:5), rnorm, n = 5)
#> , ,  = 1
#> 
#>     sd
#> mean        1        2          3         4        5
#>    1 1.363653       NA         NA        NA       NA
#>    2       NA 4.541345         NA        NA       NA
#>    3       NA       NA -0.8260046        NA       NA
#>    4       NA       NA         NA -2.701309       NA
#>    5       NA       NA         NA        NA 17.82204
#> 
#> , ,  = 2
#> 
#>     sd
#> mean         1       2        3        4     5
#>    1 0.7141121      NA       NA       NA    NA
#>    2        NA 3.92173       NA       NA    NA
#>    3        NA      NA 2.083138       NA    NA
#>    4        NA      NA       NA 10.10375    NA
#>    5        NA      NA       NA       NA 10.31
#> 
#> , ,  = 3
#> 
#>     sd
#> mean        1        2        3        4        5
#>    1 1.517669       NA       NA       NA       NA
#>    2       NA 3.537443       NA       NA       NA
#>    3       NA       NA 9.635308       NA       NA
#>    4       NA       NA       NA 6.216742       NA
#>    5       NA       NA       NA       NA 10.71347
#> 
#> , ,  = 4
#> 
#>     sd
#> mean         1        2          3        4        5
#>    1 0.8970913       NA         NA       NA       NA
#>    2        NA 4.071862         NA       NA       NA
#>    3        NA       NA -0.1250051       NA       NA
#>    4        NA       NA         NA 11.97244       NA
#>    5        NA       NA         NA       NA 10.61919
#> 
#> , ,  = 5
#> 
#>     sd
#> mean          1        2          3        4        5
#>    1 0.02593041       NA         NA       NA       NA
#>    2         NA 1.052226         NA       NA       NA
#>    3         NA       NA -0.4395716       NA       NA
#>    4         NA       NA         NA 3.383517       NA
#>    5         NA       NA         NA       NA 3.014993
#> 
maply(expand.grid(mean = 1:5, sd = 1:5), rnorm, n = 5)
#> , ,  = 1
#> 
#>     sd
#> mean         1        2          3         4         5
#>    1 0.1767388 1.830813  4.3005692 -2.872503 -4.047644
#>    2 3.1387077 2.080409  1.7663395  1.528559  6.383887
#>    3 2.9291426 2.054595 -0.5809235  5.838858  8.385583
#>    4 3.3668218 5.494057  2.5598395  6.253520 13.870784
#>    5 5.7863626 6.350489  4.9690900  5.871351  7.795530
#> 
#> , ,  = 2
#> 
#>     sd
#> mean         1          2        3          4         5
#>    1 0.4211154 -1.9045995 4.611304  0.5895879  3.403626
#>    2 3.2412631  2.2486021 3.324285  2.4491791  5.120662
#>    3 2.7278463  4.4175061 0.744830  6.0679335  8.907878
#>    4 1.9363455  0.8749631 2.185512  5.2899310 13.423312
#>    5 3.7294869  7.3067516 4.363292 10.6616492 -4.096736
#> 
#> , ,  = 3
#> 
#>     sd
#> mean        1            2         3        4         5
#>    1 2.763789  2.882412243 -3.293812 1.959838  9.022037
#>    2 2.612091  0.003134899  2.386769 2.031545 12.561386
#>    3 0.553320 -0.057917429  7.367524 1.767154  3.991960
#>    4 6.648932  4.142106719  8.380331 5.466697 -3.943103
#>    5 5.542142  1.626990515  2.280979 3.465068  6.966720
#> 
#> , ,  = 4
#> 
#>     sd
#> mean        1         2          3        4          5
#>    1 1.132992 0.3221283  5.1487326 1.243596 -6.5751226
#>    2 1.570620 4.4667801 -0.4906428 9.510975  0.2193779
#>    3 3.065487 3.4748507  0.5141894 7.048007  0.9979738
#>    4 2.846602 2.7209305  4.4490381 8.519341  1.3003842
#>    5 5.075106 3.1943701 -1.3064574 4.303655  5.2106705
#> 
#> , ,  = 5
#> 
#>     sd
#> mean        1         2          3          4         5
#>    1 1.376499 0.8488515  1.0093778 -7.7103041 -6.080120
#>    2 3.360461 2.6808490  0.4892213 10.6350262 -3.322321
#>    3 1.901491 0.3743715  3.8693234 -0.6762064  6.080771
#>    4 3.659362 2.3096085 -0.2999633  0.2340077 -1.847307
#>    5 5.558514 7.6352674 10.6800814  4.1130219 10.898321
#> 
maply(cbind(1:5, 1:5), rnorm, n = 5)
#> , ,  = 1
#> 
#>    
#>             1        2        3        4         5
#>   1 0.7430788       NA       NA       NA        NA
#>   2        NA 4.955065       NA       NA        NA
#>   3        NA       NA 7.181933       NA        NA
#>   4        NA       NA       NA 8.833542        NA
#>   5        NA       NA       NA       NA -0.655269
#> 
#> , ,  = 2
#> 
#>    
#>              1        2        3       4        5
#>   1 -0.0563361       NA       NA      NA       NA
#>   2         NA 2.144051       NA      NA       NA
#>   3         NA       NA 4.080835      NA       NA
#>   4         NA       NA       NA 3.32288       NA
#>   5         NA       NA       NA      NA 5.549967
#> 
#> , ,  = 3
#> 
#>    
#>            1        2        3        4        5
#>   1 1.198777       NA       NA       NA       NA
#>   2       NA 6.252889       NA       NA       NA
#>   3       NA       NA 4.963651       NA       NA
#>   4       NA       NA       NA 5.180119       NA
#>   5       NA       NA       NA       NA 9.264527
#> 
#> , ,  = 4
#> 
#>    
#>            1          2        3        4        5
#>   1 1.650534         NA       NA       NA       NA
#>   2       NA -0.9523938       NA       NA       NA
#>   3       NA         NA 6.156466       NA       NA
#>   4       NA         NA       NA 9.065362       NA
#>   5       NA         NA       NA       NA 3.828311
#> 
#> , ,  = 5
#> 
#>    
#>            1        2         3         4        5
#>   1 1.343913       NA        NA        NA       NA
#>   2       NA 2.815777        NA        NA       NA
#>   3       NA       NA -2.938665        NA       NA
#>   4       NA       NA        NA -0.541373       NA
#>   5       NA       NA        NA        NA 15.43344
#>