[R] I really don't understand functions in R :-)

Giovanni Petris GPetris at uark.edu
Fri Oct 20 19:01:46 CEST 2006


May I am missing something, but it seems to me that the easiest way to
solve your problem, if you don't want to change 'n', is to define

f <- function(x) x^3

If you want to allow the possibility for 'n' to change, you can
include it as an argument of 'f'

f <- function(x,n=3) x^n

Best,
Giovanni

> Date: Fri, 20 Oct 2006 14:41:26 -0200
> From: Alberto Monteiro <albmont at centroin.com.br>
> Sender: r-help-bounces at stat.math.ethz.ch
> Precedence: list
> 
> An example:
> 
> n <- 3
> f <- function(x) x^n
> f(2)
> # [1] 8
> n <- 2
> f(2)
> # [1] 4
> f
> # function(x) x^n
> 
> Ok, I know this is trivial, because function f is foverer bound 
> to the variable n. But how can I _fix_ n when I define _f_, so 
> that changing _n_ won't change the function f?
> 
> Alberto Monteiro
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 
> 

-- 

 __________________________________________________
[                                                  ]
[ Giovanni Petris                 GPetris at uark.edu ]
[ Department of Mathematical Sciences              ]
[ University of Arkansas - Fayetteville, AR 72701  ]
[ Ph: (479) 575-6324, 575-8630 (fax)               ]
[ http://definetti.uark.edu/~gpetris/              ]
[__________________________________________________]



More information about the R-help mailing list