I'd like to apply a list of programatically selected functions to each column of a data frame using dplyr
. For illustration purposes, here is my list of functions:
fun_list <- lapply(iris[-5], function(x) if(var(x) > 0.7) median else mean)
I thought this would work:
iris %>% group_by(Species) %>% summarise_each_(funs_(fun_list), names(iris)[-5])
based on ?funs_
which states the arguments should be, among other things:
A list of functions specified by ... The function itself, mean
But this fails with error:
Error in UseMethod("as.lazy") :
no applicable method for 'as.lazy' applied to an object of class "function"
It seems that funs_
is actually expecting a list of symbols that correspond to functions defined in the appropriate environment, instead of actual functions. In my application though I only get the functions, not their symbol names (besides, the functions could well be anonymous).
Is there a way to pass the actual functions to summarise_each
with dplyr
? Note I'm specifically looking for a dplyr
answer as I know how to solve this problem with other tools.
~mean
and~median
would work. But it is a formula (with environment) not a function. Instead of anonymous function you could use expressions like~sum(.)/length(.)
. – bergant