[R] generate simple function with pre-defined constants
Liviu Andronic
landronimirc at gmail.com
Thu Jun 6 17:00:27 CEST 2013
On Thu, Jun 6, 2013 at 4:48 PM, Liviu Andronic <landronimirc at gmail.com> 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.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
More information about the R-help
mailing list