# generate simple function with pre-defined constants

11 messages
Open this post in threaded view
|

## generate simple function with pre-defined constants

 Dear all, Given: a <- 2 b <- 3 I'd like to obtain the following function: f <- function(x) 2 + 3*x but when I do this: f <- function(x) a + b*x ##f ##function(x) a + b*x the 'a' and 'b' objects do not get evaluated to their constants. How could I do that? Thanks, Liviu -- Do you know how to read? http://www.alienetworks.com/srtest.cfmhttp://goodies.xfce.org/projects/applications/xfce4-dict#speed-readerDo you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: generate simple function with pre-defined constants

 On Thu, Jun 6, 2013 at 4:48 PM, Liviu Andronic <[hidden email]> wrote: > Dear all, > Given: > a <- 2 > b <- 3 > > I'd like to obtain the following function: > f <- function(x) 2 + 3*x > > but when I do this: > f <- function(x) a + b*x > ##f > ##function(x) a + b*x > > the 'a' and 'b' objects do not get evaluated to their constants. How > could I do that? > I found one solution: a <- 2 b <- 3 f <- eval(parse(text=paste("function(z)", a, "+ z * ", b))) f ##function(z) 2 + z *  3 but I still have nightmares from: > fortune("parse") If the answer is parse() you should usually rethink the question.    -- Thomas Lumley       R-help (February 2005) Is there a nicer way to approach this? Thanks, Liviu > Thanks, > Liviu > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm> http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader> Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail-- Do you know how to read? http://www.alienetworks.com/srtest.cfmhttp://goodies.xfce.org/projects/applications/xfce4-dict#speed-readerDo you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: generate simple function with pre-defined constants

 In reply to this post by Liviu Andronic HI, Not sure I understand your question:  a <- 2  b <- 3  f1<- function(x) a+b*x  f1(2) #[1] 8  f1(3) #[1] 11  f<- function(x) 2+3*x  f(2) #[1] 8  f(3) #[1] 11 A.K.   sessionInfo() R version 3.0.0 (2013-04-03) Platform: x86_64-unknown-linux-gnu (64-bit) locale:  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8     [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8    [7] LC_PAPER=C                 LC_NAME=C                  [9] LC_ADDRESS=C               LC_TELEPHONE=C            [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       attached base packages: [1] stats     graphics  grDevices utils     datasets  methods   base     other attached packages: [1] arrayhelpers_0.76-20120816 abind_1.4-0               [3] plyr_1.8                   stringr_0.6.2             [5] reshape2_1.2.2            loaded via a namespace (and not attached): [1] tools_3.0.0 ----- Original Message ----- From: Liviu Andronic <[hidden email]> To: "[hidden email] Help" <[hidden email]> Cc: Sent: Thursday, June 6, 2013 10:48 AM Subject: [R] generate simple function with pre-defined constants Dear all, Given: a <- 2 b <- 3 I'd like to obtain the following function: f <- function(x) 2 + 3*x but when I do this: f <- function(x) a + b*x ##f ##function(x) a + b*x the 'a' and 'b' objects do not get evaluated to their constants. How could I do that? Thanks, Liviu -- Do you know how to read? http://www.alienetworks.com/srtest.cfmhttp://goodies.xfce.org/projects/applications/xfce4-dict#speed-readerDo you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: generate simple function with pre-defined constants

Open this post in threaded view
|

## Re: generate simple function with pre-defined constants

 In reply to this post by Liviu Andronic Hi, I am not sure what you are looking for. Here are some examples: foo <- function(a,b,x) a + b*x > foo function(a,b,x) a + b*x a <- 2 b <- 3 x <- 0:10 > foo(a,b,x)  [1]  2  5  8 11 14 17 20 23 26 29 32 Or library(polynom) p1 <- polynomial(c(a,b)) > p1 2 + 3*x f1 <- as.function(p1) > f1(x)  [1]  2  5  8 11 14 17 20 23 26 29 32 Regards, Pascal 2013/6/6 Liviu Andronic <[hidden email]> > Dear all, > Given: > a <- 2 > b <- 3 > > I'd like to obtain the following function: > f <- function(x) 2 + 3*x > > but when I do this: > f <- function(x) a + b*x > ##f > ##function(x) a + b*x > > the 'a' and 'b' objects do not get evaluated to their constants. How > could I do that? > > Thanks, > Liviu > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm> http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader> Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail> > ______________________________________________ > [hidden email] 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. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: generate simple function with pre-defined constants

Open this post in threaded view
|

## Re: generate simple function with pre-defined constants

Open this post in threaded view
|

## Re: generate simple function with pre-defined constants

Open this post in threaded view
|