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

Kevin E. Thorpe kevin.thorpe at utoronto.ca
Fri Oct 20 18:57:07 CEST 2006


Is this what you want?

> f <- function(x,n=3) x^n
> f(2)
[1] 8
> n <- 2
> f(2)
[1] 8
> f(2,2)
[1] 4


Alberto Monteiro wrote:
> 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


-- 
Kevin E. Thorpe
Biostatistician/Trialist, Knowledge Translation Program
Assistant Professor, Department of Public Health Sciences
Faculty of Medicine, University of Toronto
email: kevin.thorpe at utoronto.ca  Tel: 416.946.8081  Fax: 416.946.3297



More information about the R-help mailing list