[R] Function to Define a Function
S Ellison
S.Ellison at lgc.co.uk
Tue Aug 10 19:04:58 CEST 2010
Neat. But why assign the functions to separate variables at all?
mdlChooser <- function(type=c("one","two")) {
type <- match.arg(type)
m <- switch(type,
one=function(x,N0,r) N0*exp(x*r) ,
two=function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r))
)
m
}
also works without appearing to assign different functions to the same
variable.
(In this simple example, you wouldn't need the m<- assignment either;
you could simply let the switch return its result. But I assume the real
intended use is more complicated than just returning the function)
>>> Derek Ogle <DOgle at northland.edu> 10/08/2010 13:48:13 >>>
Gabor ... that worked perfectly. Thank you.
> -----Original Message-----
> Try this:
>
> mdlChooser <- function(type = c("one", "two")) {
> one <- function(x,N0,r) N0*exp(x*r)
> two <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r))
> type <- match.arg(type)
> get(type)
> }
______________________________________________
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.
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
More information about the R-help
mailing list