lapply - R: sequentially applying an arbitrarty list of functions with arguments to a matrix -
lapply - R: sequentially applying an arbitrarty list of functions with arguments to a matrix -
i have list of filtering functions f1,f2,f3,f4,....
take matrix m
, number of options input , homecoming subset of rows of matrix output. able define in orderly way meta-filtering function settings metaf1, metaf2, metaf3,...
specify sequential application of specified nr of filtering functions, e.g. first f2
, f3
, using given options each. store these filtering settings in list of class "metafiltering"
, , have function apply filtering steps specified in given metafiltering
object. thought able in way allow filtering settings stored , applied in orderly way. how accomplish in elegant way in r? or there perhaps other convenient methods accomplish this?
edit: give example, have matrix
m=replicate(10, rnorm(20))
and filtering functions (these examples, mine more complicated :-) )
f1=function(m,opt1,opt2) { return(m[(m[,2]>opt1)&(m[,1]>opt2),]) } f2=function(m,opt1) { return(m[(m[,3]>opt1),]) }
and have defined next metafiltering
settings of specific class specify 2 functions have applied sequentially matrix m
metafilterfuncs=list(fun1=f1(opt1=0.1,opt2=0.2),fun2=f2(opt1=0.5)) class("metafilterfuncs")="metafiltering"
the question have how apply filtering steps of arbitrary metafiltering
function object given matrix m using specified functions , settings?
you can :
you define sort of functions pieplines give priority each function.
pipelines <- c(f1=100,f2=300,f3=200)
i define 3 dummy functions here test:
f1 <- function(m,a) m + f2 <- function(m,b) m + b f3 <- function(m,c) m + c
for each function , store argument in list :
args <- list(f1=c(a=1),f2=c(b=2),f3=c(c=3))
then apply functions :
m <- matrix(1:2,ncol=2) (func in names(pipelines[order(pipelines)])) { m <- do.call(func,list(m,args[[func]])) }
r lapply
Comments
Post a Comment