[R] generate simple function with pre-defined constants

Pascal Oettli
Thu, 06 Jun 2013 15:34:29 -0000

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 <landronimirc@gmail.com>

> 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
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]