You want to apply multiple functions to a dataframe with map(), but (apparently) there is no map() variation that does exactly this, only parts of it. For the multiple function part we have invoke_map() and for the multiple argument part over a dataframe we have pmap().
invoke_map()
allows the use of multiple functions at once. For example, if we want to generate 5 random variates for a uniform and normal distributions, the code is:
func <- list(runif, rnorm)
invoke_map(func, n = 5)
pmap()
is just like map, but it allows to pass multiple arguments to a single function. For example, if we want to generate 10 random variates from a normal distribution with mean = 0 and sd = 1, but also 100 random variates from a normal distribution with mean = 100 and sd = 20, the code looks like this:
args <- list(mean = c(0, 100), sd = c(1, 20), n = c(10, 100))
pmap(args, rnorm)
To solve your question, we have to combine both functions in the following way:
fun <- function(f) pmap(list(x = mtcars, na.rm = TRUE), f)
param <- list(list(mean), list(median))
invoke_map(.f = fun, .x = param)
How does this work?
At the invoke_map() level, fun
takes as arguments param
, which are the functions we want to apply to mtcars
.
Next, at the fun
level, these functions stored in param
are applied by pmap()
, one at a time, to each column in mtcars
.
Note: For the solution to really make sense, keep in mind the arguments invoke_map() and pmap() take.
More info about how invoke_map() and pmap() work.