Re: [R] understanding the FUNCTION function

2012-04-26 Thread Wet Bell Diver
In addition: the object MyNumberIs *is* created, but inside the environment attached to the function as you evaluate it. And this environment is gone when the function is done. (there is more to it, but this is basically how it works). This is what Michael refers to in his answer. So, when

Re: [R] understanding the FUNCTION function

2012-04-26 Thread Patrick Burns
I suspect you are trying to find your way into Circle 6 of 'The R Inferno' but haven't yet got in. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf Pat On 26/04/2012 03:06, michaelyb wrote: Hello, I am trying to understand why the FUNCTION used in several codes, won't create the object

Re: [R] understanding the FUNCTION function

2012-04-26 Thread michaelyb
Peter, your solution is actually very interesting. I have never seen or heard of before. I will look into it. Meanwhile, look at this example instead: fac-function(x){a-1 for(i in 1:x){ a-a*i print(a)}} The result is : fac(5) [1] 1 [1] 2 [1] 6

Re: [R] understanding the FUNCTION function

2012-04-26 Thread Ista Zahn
On Thu, Apr 26, 2012 at 8:56 AM, michaelyb cel81009...@gmail.com wrote: Peter, your solution is actually very interesting. I have never seen or heard of before. I will look into it. Meanwhile, look at this example instead: fac-function(x){a-1                 for(i in 1:x){                

Re: [R] understanding the FUNCTION function

2012-04-26 Thread William Dunlap
26, 2012 6:44 AM To: michaelyb Cc: r-help@r-project.org Subject: Re: [R] understanding the FUNCTION function On Thu, Apr 26, 2012 at 8:56 AM, michaelyb cel81009...@gmail.com wrote: Peter, your solution is actually very interesting. I have never seen or heard of before. I will look

Re: [R] understanding the FUNCTION function

2012-04-26 Thread michaelyb
Any solution for that type of problem? I did read the ?-, and seems very similar to the assign function, if I am not mistaken -- View this message in context: http://r.789695.n4.nabble.com/Using-FUNCTION-to-create-usable-objects-tp4588681p4590445.html Sent from the R help mailing list

Re: [R] understanding the FUNCTION function

2012-04-26 Thread R. Michael Weylandt
Yes, don't do it. I know this can seem appealing if you're coming from another language, but playing fast and loose with globals, non-local effects, and superassignment is almost-never a good idea. Michael On Thu, Apr 26, 2012 at 1:32 PM, michaelyb cel81009...@gmail.com wrote: Any solution for

Re: [R] understanding the FUNCTION function

2012-04-26 Thread Ista Zahn
On Thu, Apr 26, 2012 at 1:32 PM, michaelyb cel81009...@gmail.com wrote: Any solution for that type of problem? Solution for what type of problem? I did read the ?-, and seems very similar to the assign function, if I am not mistaken Bottom line: don't use - until you know what you are

Re: [R] understanding the FUNCTION function

2012-04-26 Thread michaelyb
Ista, Since you seem to know your stuff very well, how would you get 120 out of a function that gives you the factorial of 5, without using factorial(5)? Meanwhile, look at this example instead: fac-function(x){a-1 for(i in 1:x){ a-a*i

Re: [R] understanding the FUNCTION function

2012-04-26 Thread R. Michael Weylandt
Simply return it like a function is supposed to: fac - function(x, loud = TRUE){ a - 1 for(i in seq_len(x)) { # seq_len is faster and more robust a - a * i if(loud) print(a) } return(a) } fac3 - fac(3) print(fac3) # As desired Michael On Thu, Apr 26, 2012 at

Re: [R] understanding the FUNCTION function

2012-04-26 Thread David Winsemius
On Apr 26, 2012, at 2:16 PM, michaelyb wrote: Ista, Since you seem to know your stuff very well, how would you get 120 out of a function that gives you the factorial of 5, without using factorial(5)? Meanwhile, look at this example instead: fac-function(x){a-1 for(i in

Re: [R] understanding the FUNCTION function

2012-04-26 Thread Sarah Goslee
In R, the preferred method is to assign the result to a new object: fac - function(x) { a-1 for(i in 1:x){ a-a*i print(a) } a # need to explicitly state what the function should return } myresult - fac(5) myresult Sarah On Thu, Apr 26, 2012 at 2:16 PM, michaelyb

Re: [R] understanding the FUNCTION function

2012-04-26 Thread Jeff Newmiller
the solution is to write functions that return the data you want changed, and let the caller of the function decide where to put the answer. If you want to return multiple answers, collect them in a vector or list and return that.

Re: [R] understanding the FUNCTION function

2012-04-26 Thread Rui Barradas
Hello, gives you 120, but you cannot access it after the end of execution. Because you're just printing the final value of 'a', not returning it. fac - function(x){ a - 1 for(i in 1:x) a - a*i a } The return value must be the last instruction in a function. Then, if

Re: [R] understanding the FUNCTION function

2012-04-26 Thread michaelyb
David - My question to you may sound (actually, it really is) silly, but please do take your time to answer it. What is the difference between: fac-function(x){a-1 for (i in 1:x){ a-a*i }a} and: fac-function(x){a-1 for (i

Re: [R] understanding the FUNCTION function

2012-04-26 Thread David Winsemius
On Apr 26, 2012, at 3:40 PM, michaelyb wrote: David - My question to you may sound (actually, it really is) silly, but please do take your time to answer it. What is the difference between: fac-function(x){a-1 for (i in 1:x){ a-a*i }a} and:

[R] understanding the FUNCTION function

2012-04-25 Thread michaelyb
Hello, I am trying to understand why the FUNCTION used in several codes, won't create the object after it finishes running the code. For instance, look at the following: Number- function(x) {MyNumberIs-x} When I run Number(5) Everything goes well, except that if I try to call the object

Re: [R] understanding the FUNCTION function

2012-04-25 Thread R. Michael Weylandt michael.weyla...@gmail.com
Functions are not subroutines; that is, the effects (including assignments) are not global. You need something like F - function(x) x A - F(3) But that seems unnecessary... A - 3 Michael On Apr 25, 2012, at 10:06 PM, michaelyb cel81009...@gmail.com wrote: Hello, I am trying to