R-helpers: If I want to pass a character name of a function TO a function, and then have that function executed, how would I do this? I want an arbitrary version of the following, where any function can be used (e.g. I don't want the if-then statement here): apply_some_function <- function(data,function_name) { if(function_name=="mean") { return(mean(data)) } if(function_name=="min") { return(min(data)) } } apply_some_function(1:10,"mean") apply_some_function(1:10,"min") Basically, I want the character name of the function used to actually execute that function. Thanks! --j [[alternative HTML version deleted]]
Hi, I think the easiest way is with match.fun(). For instance: apply_some_function <- function(data, function_name) { FUN <- match.fun(function_name) FUN(data) }> apply_some_function(1:10,"mean")[1] 5.5> apply_some_function(1:10,"min")[1] 1 Cheers, Josh On Wed, Sep 22, 2010 at 4:06 PM, Jonathan Greenberg <greenberg at ucdavis.edu> wrote:> R-helpers: > > If I want to pass a character name of a function TO a function, and then > have that function executed, how would I do this? ?I want > an arbitrary version of the following, where any function can be used (e.g. > I don't want the if-then statement here): > > apply_some_function <- function(data,function_name) > { > ?if(function_name=="mean") > { > return(mean(data)) > } > if(function_name=="min") > { > return(min(data)) > } > > } > > apply_some_function(1:10,"mean") > apply_some_function(1:10,"min") > > Basically, I want the character name of the function used to actually > execute that function. ?Thanks! > > --j > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
One possibility: R> f = function(x, f) eval(as.call(list(as.name(f), x))) R> f(1:10, "mean") [1] 5.5 R> f(1:10, "max") [1] 10 Andy From: Jonathan Greenberg> R-helpers: > > If I want to pass a character name of a function TO a > function, and then > have that function executed, how would I do this? I want > an arbitrary version of the following, where any function can > be used (e.g. > I don't want the if-then statement here): > > apply_some_function <- function(data,function_name) > { > if(function_name=="mean") > { > return(mean(data)) > } > if(function_name=="min") > { > return(min(data)) > } > > } > > apply_some_function(1:10,"mean") > apply_some_function(1:10,"min") > > Basically, I want the character name of the function used to actually > execute that function. Thanks! > > --j > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Notice: This e-mail message, together with any attachme...{{dropped:11}}
Are you aware that you can pass the function, itself, as an argument (R is mostly a functional programming language where language objects are first class objects). e.g> g <- function(x,fun)fun(x) > g(2,function(x)x^2)[1] 4 On Wed, Sep 22, 2010 at 4:06 PM, Jonathan Greenberg <greenberg at ucdavis.edu> wrote:> R-helpers: > > If I want to pass a character name of a function TO a function, and then > have that function executed, how would I do this? ?I want > an arbitrary version of the following, where any function can be used (e.g. > I don't want the if-then statement here): > > apply_some_function <- function(data,function_name) > { > ?if(function_name=="mean") > { > return(mean(data)) > } > if(function_name=="min") > { > return(min(data)) > } > > } > > apply_some_function(1:10,"mean") > apply_some_function(1:10,"min") > > Basically, I want the character name of the function used to actually > execute that function. ?Thanks! > > --j > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Bert Gunter Genentech Nonclinical Biostatistics