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
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.
Cheers
Allan
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] 4The 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
Hi!
I'm no guru in R. But I can think of 2 ways (have to
be tried):
1) As Uwe Ligges said: just save return the stored
variable a<-preprocess(xdata) and return it (if you
want to return more than 1 item use list) and give
this variable to the next function. Example:
func1<-function(x)
{
y<-x^2
# for testing:
print(paste("func1", x, y))
return(y)
}
func2<-function(x, valuefunc1=NA)
{
if (is.na(valuefunc1)) valuefunc1<-func1(x)
# calculate things with valuefunc1
print(paste("func2", x, valuefunc1))
return(list(valuefunc1=valuefunc1))
}
func3<-function(x, valuefunc1=NA)
{
if (is.na(valuefunc1)) valuefunc1<-func1(x)
# calculate things with valuefunc1
print(paste("func3", x, valuefunc1))
return(list(valuefunc1=valuefunc1))
}
# use this
a<-list(NA)
names(a)<-c("valuefunc1")
a<-func2(x=3, valuefunc1=a$valuefunc1)
a<-func3(x=3, valuefunc1=a$valuefunc1) # be careful,
makes only sense if the x is equal to former fuction
call...
a<-func3(x=999, valuefunc1=a$valuefunc1) # will be the
same
a<-func3(x=999) # will be different
2) use global variables like
assign("stored.value", x, envir=.GlobalEnv)
example see reply of my posting:
RE: [R] LOCF - Last Observation Carried Forward Simon
Fear (Sat 15 Nov 2003 - 03:28:03 EST)
HTH,
Karl