[R]: global and local variables

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Dec 9 13:06:08 CET 2003


allan clark wrote:

>    Hi all
> 
>    I have a problem pertaining to local and global variables.
> 
>    Say I have a function defined as follows:
> 
>    a<-function(x)
>    {y<x^2}
> 
>    i.e
>    a(2)
>    [1] 4

The function a specified above won't return 4!



>    function b is now defined to take the value of y and do some
>    manipulation with it. As it stands I dont know how to store the
>    variable y such that other functions are able to reference its value.
> 
>    I know that I can simply put the operations found in b simply into a
>    but this is not what I want.
> 
>    I would like to have stand alone functions say
> 
>    a, b and c which could be run independently as well as have a function
>    called say
> 
>    control that can run a, b and c.
> 
>    i.e.
> 
>    control<- function( x)
>    {
>    a(x)
>    b(x)
>    c(x)
>    }
> 
>    I hope that you guys understand what I'm trying to do.

You are trying to read "An Introduction to R"???
If not, please try!

What you are really going to do: using assigments and return() 
statements as in:

a <- function(x) return(x^2)

foo <- function(x) {
   y <- a(x)
   z <- b(x)
   return(list(y=y, z=z))
}

foo(.....)

Uwe Ligges


>    Cheers
>    Allan
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list