R/implicitExpansion.R
mmapply.Rd
Similar function to mapply
with support for implicit expansion.
mmapply(
FUN,
...,
MoreArgs = list(),
SIMPLIFY = TRUE,
USE.NAMES = TRUE,
ALLOW.RECYCLING = FALSE
)
Function to apply to each combination of arguments. Found via match.fun
.
Objects that are coerced to arrays, expanded using implicit expansion, and then vectorized over.
Pass arguments in a list instead of ...
If TRUE
, the resulting list array is simplified to an atomic array if possible.
If TRUE
, the dimensions are named using the names of input arguments of matching size.
Whether to allow recycling of elements in each dimension.
An array containing the result of FUN
for each combination of entries from ...
after implicit expansion.
Most arguments are handled in a similar fashion to mapply
with some key differences:
Entries of MoreArgs
are treated the same as the ones in ...
, i.e.
mmapply(...)
is the same as mmapply(MoreArgs = list(...))
.
Additional arguments to FUN can be passed here or in ...
, either as they are
(if they are atomic), or as a list()
of length one.
SIMPLIFY
only simplifies a list array to an atomic array, nothing else.
USE.NAMES
uses names from all arguments, but never uses an argument itself as names.
If ALLOW.RECYCLING
is set to TRUE
, all arrays of any size are compatible.
summaries <- list(Max = max, Min = min, avg = mean)
data <- list(a = 1:5, b = 2:3, c = 20:12)
formatStrings <- array(c('%.1f', '%.3f'), c(1,1,2))
mmapply(function(f, d, s) sprintf(s, f(d)), summaries, t(data), formatStrings)
#> , , 1
#>
#> a b c
#> Max "5.0" "3.0" "20.0"
#> Min "1.0" "2.0" "12.0"
#> avg "3.0" "2.5" "16.0"
#>
#> , , 2
#>
#> a b c
#> Max "5.000" "3.000" "20.000"
#> Min "1.000" "2.000" "12.000"
#> avg "3.000" "2.500" "16.000"
#>