Pass a data frame to a list of functions?

Hi
I understand that map() takes a data frame and iterates a single function over it's every object. but what if instead of a single function, I wanted to iterate a list of functions over every object of a data frame?

the nearest thing I found in "R for Data science" is invoke_map()
invoke_map() passes a list of arguments to a list of cognate functions.

but my problem is that while I can pass a single vector to a list of functions using invoke_map(), I can't pass a data frame to it.

for example lets say I want to pass this data frame:

df <- tibble(
  a = rnorm(10),
  b = rnorm(10),
  c = rnorm(10),
  d = rnorm(10)
)

to this list of functions:
f <- c("mean", "median", "IQR")

I can do it separately for each object:

invoke_map(f, x = df$a)

but if I'm supposed to do it for each object of data frames, it'll quickly become tedious.

So is there any function in tidyverse that allows you to pass a data frame(or even better, a list of data frames) to a list of functions?

Have you looked at optional parameters with the elipse (...) placeholder?

I did but couldn't find anything useful

library(tidyverse)
df <- tibble(
  a = rnorm(10),
  b = rnorm(10),
  c = rnorm(10),
  d = rnorm(10)
)
f <- c("mean", "median", "IQR")

results <- map_dfr(df,
    ~invoke_map_dbl(f, x = .)
)

results$metric <- f

results
# A tibble: 3 x 5
       a       b       c      d metric
   <dbl>   <dbl>   <dbl>  <dbl> <chr> 
1 -0.257  0.0854  0.0328 -0.545 mean  
2 -0.111 -0.0732 -0.372  -0.629 median
3  1.22   1.78    1.72    1.52  IQR
1 Like

Oh...that was nice.
thanks a lot

Thanks for the interesting question. I hadn't used invoke_map before

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.