[R] Trying to learn how to write a function... can't define a variable??

wwreith reith_william at bah.com
Fri Sep 7 20:00:15 CEST 2012


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.



More information about the R-help mailing list