Hello, I'm a new user... I have a function : calculate <- function(x,y) { z <- x + y } I would like to use the result (z) with another function : recalculate <- function(...) { a <- z^2 } But R says that z does not exist... How can I use z in an another function ? Thank you for your answer... -- David [[alternative HTML version deleted]]
First of all, you might try reading the manual. Second, you might try something like this: calculate <- function(x,y) { z <- x + y z } recalculate(z) { a <- z^2 a } z <- calculate(x, y) recalculate(z) You need to return some value from your functions, and you need to assign that value to a variable. Sarah -- Sarah Goslee http://www.stringpage.com [[alternative HTML version deleted]]
Her you just make the functions. R> calculate <- function(x,y){z <- x + y} R> recalculate <- function(z){a <- z^2} You should run the functions, by take z as output for the first function ans z as input for the next function: R> calculate <- function(x,y){z <- x + y} R> recalculate <- function(z){a <- z^2} R> z <- calculate(1,2) R> a <- recalculate(z) R> z [1] 3 R> a [1] 9 Good luck, Kristel David Hajage wrote:> Hello, > > I'm a new user... > > I have a function : > > calculate <- function(x,y) > { > z <- x + y > } > I would like to use the result (z) with another function : > > recalculate <- function(...) > { > a <- z^2 > } > > But R says that z does not exist... > > How can I use z in an another function ? > > Thank you for your answer... > > -- > David > > [[alternative HTML version deleted]] > > ______________________________________________ > 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-- __________________________________________ Kristel Joossens Ph.D. Student Research Center ORSTAT K.U. Leuven Naamsestraat 69 Tel: +32 16 326929 3000 Leuven, Belgium Fax: +32 16 326732 E-mail: Kristel.Joossens at econ.kuleuven.be http://www.econ.kuleuven.be/public/ndbae49 Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
David Hajage wrote:> Hello, > > I'm a new user... > > I have a function : > > calculate <- function(x,y) > { > z <- x + y# insert: z> } > I would like to use the result (z) with another function : > > recalculate <- function(...) > { > a <- z^2# insert: a> }Type: recalculate(calculate(3,4)) Please read "An Introduction to R" as well as the posting guide! Uwe Ligges> But R says that z does not exist... > > How can I use z in an another function ? > > Thank you for your answer... > > -- > David > > [[alternative HTML version deleted]] > > ______________________________________________ > 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
On 06-Dec-05 David Hajage wrote:> Hello, > > I'm a new user... > > I have a function : > > calculate <- function(x,y) > { > z <- x + y > } > I would like to use the result (z) with another function : > > recalculate <- function(...) > { > a <- z^2 > } > > But R says that z does not exist... > > How can I use z in an another function ? > > Thank you for your answer...With 'calculate' as written, z is "internal" to 'calculate' and is not visible from outside (and the internal assignment to z will not affact the value of a variable also called z outside the function). The simplest way to extract the calculated value is to "return" it from the function and assign it to z outside the function: calculate <- function(x,y) { return(x + y) } z<-calculate(x,y) and then say a<-recalculate(z) where, again, you need to "get a out of" the function, so recalculate <- function(...) { return(z^2) } While it is possible to change the values of "external" variables from within functions, this is not a recommended way to proceed, since it depends on the named variable inside the function meaning the same as the variable with the same name outside the function. Since the purpose of defining functions is to have something which is re-usable in different contexts, it is generally desriable to make function definitions independent of the environment from which they may be called. Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 06-Dec-05 Time: 18:25:53 ------------------------------ XFMail ------------------------------