[R] generate simple function with pre-defined constants

Liviu Andronic landronimirc at gmail.com
Thu Jun 6 17:18:07 CEST 2013


On Thu, Jun 6, 2013 at 5:03 PM, arun <smartpink111 at yahoo.com> wrote:
> HI,
> Not sure I understand your question:
>  a <- 2
>  b <- 3
>  f1<- function(x) a+b*x
>
I don't want the function to depend on the objects a and b, but
instead use the values of those objects (I do this within a function).

Liviu


>  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 <landronimirc at gmail.com>
> To: "r-help at r-project.org Help" <r-help at r-project.org>
> 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.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 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.
>



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