SH.Chou
2009-Aug-27 20:41 UTC
[R] select one function from two of them in another function
Hi all, I have two functions called test1() and test2(). Now how do I select one of them in test3()?? Say test3<-function(func="test1"){ if (func=="test1"){ now.func<-test1() } else now.func<-test2() } I know this function I wrote does not right. Do anyone can tell me how to do that for real? Thanks a million S.H. -- ====================================Shih-Hsiung, Chou Department of Industrial Manufacturing and Systems Engineering Kansas State University -- ====================================Shih-Hsiung, Chou Department of Industrial Manufacturing and Systems Engineering Kansas State University [[alternative HTML version deleted]]
jim holtman
2009-Aug-27 20:57 UTC
[R] select one function from two of them in another function
Is this what you want -- this returns a function that you can then call:> test1 <- function() 1 > test2 <- function() 2 > test3 <- function(func='test1'){ # return the function to call+ if (func == 'test1') return(test1) + return(test2) + }> > # test it > test3()() # default -- notice the second set of parens[1] 1> > test3('test1')()[1] 1> > test3('test2')()[1] 2> > >On Thu, Aug 27, 2009 at 4:41 PM, SH.Chou<cls3415 at gmail.com> wrote:> Hi all, ? ? ?I have two functions called test1() and test2(). Now how do I > select one of them in test3()?? > > Say > ?test3<-function(func="test1"){ > ? ? ? ?if (func=="test1"){ > ? ? ? ? ? ? ? now.func<-test1() > ? ? ? } > ? ? ? else now.func<-test2() > ?} > > I know this function I wrote does not right. Do anyone can tell me how to do > that for real? > > Thanks a million > > S.H. > > -- > ====================================> Shih-Hsiung, Chou > Department of Industrial Manufacturing > and Systems Engineering > Kansas State University > > > > -- > ====================================> Shih-Hsiung, Chou > Department of Industrial Manufacturing > and Systems Engineering > Kansas State University > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?