wwreith
2012-Sep-07 18:00 UTC
[R] Trying to learn how to write a function... can't define a variable??
I am just starting to experiment with writing a function and have run into what seems like a limitation or more likely a lack of understanding on my part. Very Simple Example: I want to define a function that does 1+1=2. z<-1 ADD<-function(x) { x<-x+1 } ADD(z) z output for z is 1 not the expected 2. Now if I were to do print(x+1) instead of x<-x+1 it does return 2, so the function seems ok with x+1, but not ok with x<-. Is there a way to define a variable inside a function or am I violating some rule that I don't know about? Thanks for the help! Will -- View this message in context: http://r.789695.n4.nabble.com/Trying-to-learn-how-to-write-a-function-can-t-define-a-variable-tp4642528.html Sent from the R help mailing list archive at Nabble.com.
Bert Gunter
2012-Sep-07 19:05 UTC
[R] Trying to learn how to write a function... can't define a variable??
WHEW! Have you read An Introduction to R? If not, stop and do so. If you have, stop and do so again. Or look for other web tutorials (you may get some suggestions here) or books (CRAN is a good place to look for this). The problem is that you are so far from understanding how a function works, that I wouldn't know where to begin. Ergo the tutorial suggestions. May I also suggest that should anyone else care to have a go at helping, that they reply privately, as this somehow does not seem the right venue for such basic tutorials. Cheers, Bert On Fri, Sep 7, 2012 at 11:00 AM, wwreith <reith_william at bah.com> wrote:> I am just starting to experiment with writing a function and have run into > what seems like a limitation or more likely a lack of understanding on my > part. > > Very Simple Example: I want to define a function that does 1+1=2. > > z<-1 > ADD<-function(x) > { > x<-x+1 > } > ADD(z) > z > output for z is 1 not the expected 2. > > Now if I were to do print(x+1) instead of x<-x+1 it does return 2, so the > function seems ok with x+1, but not ok with x<-. Is there a way to define a > variable inside a function or am I violating some rule that I don't know > about? > > Thanks for the help! > > Will > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Trying-to-learn-how-to-write-a-function-can-t-define-a-variable-tp4642528.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Rui Barradas
2012-Sep-07 19:16 UTC
[R] Trying to learn how to write a function... can't define a variable??
Hello, Start by (re-)reading An Introduction to R, file R-intro.pdf in your doc directory. Chapter 10. You're just computing, not returning the result of that computation. Nor anything else, ADD() does not return a value. (See the example in R-intro, section 10.1) And you need to assign the return value to change the value of 'z' _outside_ the function. R passes arguments by value, what happens inside functions stays inside functions. (R-intro, section 10.5, first sentence.) # Right way z <- 1 ADD <- function(x){ x <- x + 1 x } ADD(z) # returns 2 z # still 1 z2 <- ADD(z) z2 # equals 2 Hope this helps, Rui Barradas Em 07-09-2012 19:00, wwreith escreveu:> I am just starting to experiment with writing a function and have run into > what seems like a limitation or more likely a lack of understanding on my > part. > > Very Simple Example: I want to define a function that does 1+1=2. > > z<-1 > ADD<-function(x) > { > x<-x+1 > } > ADD(z) > z > output for z is 1 not the expected 2. > > Now if I were to do print(x+1) instead of x<-x+1 it does return 2, so the > function seems ok with x+1, but not ok with x<-. Is there a way to define a > variable inside a function or am I violating some rule that I don't know > about? > > Thanks for the help! > > Will > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Trying-to-learn-how-to-write-a-function-can-t-define-a-variable-tp4642528.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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.
Berend Hasselman
2012-Sep-07 19:18 UTC
[R] Trying to learn how to write a function... can't define a variable??
On 07-09-2012, at 20:00, wwreith wrote:> I am just starting to experiment with writing a function and have run into > what seems like a limitation or more likely a lack of understanding on my > part. > > Very Simple Example: I want to define a function that does 1+1=2. > > z<-1 > ADD<-function(x) > { > x<-x+1 > } > ADD(z) > z > output for z is 1 not the expected 2. >expected ==> "desired" Your ADD is not returning the new value of x. Either ADD<-function(x) { x<-x+1 x # return new value of x } or ADD<-function(x) { x<-x+1 return(x) # return new value of x } or even ADD<-function(x) { (x<-x+1) } Have a look in the R-intro manual section "Writing your own functions" Berend
David Winsemius
2012-Sep-07 20:34 UTC
[R] Trying to learn how to write a function... can't define a variable??
On Sep 7, 2012, at 11:00 AM, wwreith wrote:> I am just starting to experiment with writing a function and have run into > what seems like a limitation or more likely a lack of understanding on my > part. > > Very Simple Example: I want to define a function that does 1+1=2. > > z<-1 > ADD<-function(x) > { > x<-x+1 > } > ADD(z) > z > output for z is 1 not the expected 2. > > Now if I were to do print(x+1) instead of x<-x+1 it does return 2, so the > function seems ok with x+1, but not ok with x<-. Is there a way to define a > variable inside a function or am I violating some rule that I don't know > about?The rule you are violating is failing to assign the calculated value in the proper environment. The x=1 value exists inside the function and _is_ returned, but you didn't do anything with it, so it has no name and will get garbage collected. Here's an incrementer function that works: ADD <- function(x) assign( deparse(substitute(x)), x+1, envir=parent.frame() ) x=1 ADD(x) x #[1] 2 You could also have written it thusly: ADD <- function(x) x <<- x+1 ) (But that operator is frowned upon by those in the know.) I'm not sure what sort of reaction would be provoked by: ADD <- function(x) { eval.parent(substitute(x <- x + 1)) } The data.table package does in-memory alterations in its objects using a database model. It is often much faster than reassignment of dataframes to them self or even adding a columns, which does require making a copy (or maybe even two) of the entire object. -- David Winsemius, MD Alameda, CA, USA