While correct, the solution below is unnecessarily complicated. Functions can be passed around and used as arguments just like any other objects. Hence (using Jim's example):
> test3 <- function(func)func > test3(test1)() [1] 1 > test3(test2)() [1] 2 Of course. arguments can be entered in the parentheses as usual: > test3(function(x)x^2) (5) [1] 25 Bert Gunter Genentech Nonclinical Biostatisics -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of jim holtman Sent: Thursday, August 27, 2009 1:58 PM To: SH.Chou Cc: r-help@r-project.org Subject: Re: [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<cls3...@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@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? ______________________________________________ R-help@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. ______________________________________________ R-help@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.