How to apply combination of multiple functions in R in a smart manner? -


lets assume have 4 functions. these sample functions. in real scenario functions more complicated.

f1= function(data){   data1= data*2   return (data1) }  f2= function(data){   data1= data*4   return (data1) }  f3= function(data){   data1= data*data   return (data1) }  f4= function(data){   data1= data**5   return (data1) }  data = matrix(1:100,10,10) 

now want apply combination of these function , see output of each

f1 f2 f1 f3 f1 f4 ... f1 f2 f3,  f1 f2 f4....  f1 f2 f3 f4. 

my question how apply combination of these functions smartly.

first, put functions in list

ff <- list(f1=f1,f2=f2,f3=f3,f4=f4) 

then here's helper function generate list of possible combinations of elements list

allcomb <- function(x) {     do.call("c", lapply(seq_along(x), function(n) combn(x,n, simplify=false))) } 

then, can generate lists of functions can pass reduce() apply them sequentially data (and i've added setnames() identify each result came from)

setnames(     lapply(        allcomb(ff),        function(x) reduce(function(d,f) f(d), x, init=data)     ),     sapply(allcomb(names(ff)), paste, collapse=":") ) 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -