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 MyNumberIs, I won't find it. I understand that this function can assume many parameters, but why won't it create the object? Besides, if I try "assing", it won't work either, no matter how I do it. Any advice/explanation? Thankk you very much! -- View this message in context: http://r.789695.n4.nabble.com/understanding-the-FUNCTION-function-tp4588681p4588681.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-Apr-26 04:07 UTC
[R] understanding the FUNCTION function
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 <cel81009759 at gmail.com> wrote:> 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 MyNumberIs, I > won't find it. > > I understand that this function can assume many parameters, but why won't it > create the object? > > Besides, if I try "assing", it won't work either, no matter how I do it. > > Any advice/explanation? > > Thankk you very much! > > -- > View this message in context: http://r.789695.n4.nabble.com/understanding-the-FUNCTION-function-tp4588681p4588681.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.
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 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 MyNumberIs, I > won't find it. > > I understand that this function can assume many parameters, but why won't it > create the object? > > Besides, if I try "assing", it won't work either, no matter how I do it. > > Any advice/explanation? > > Thankk you very much! > > -- > View this message in context: http://r.789695.n4.nabble.com/understanding-the-FUNCTION-function-tp4588681p4588681.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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
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 2:16 PM, michaelyb <cel81009759 at gmail.com> 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 1:x){ >> ? ? ? ? ? ? ? ? a<-a*i >> ? ? ? ? ? ? ? ? print(a)}} > > gives you 120, but you cannot access it after the end of execution. > > > -- > View this message in context: http://r.789695.n4.nabble.com/Using-FUNCTION-to-create-usable-objects-tp4588681p4590549.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.
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 1:x){ >> a<-a*i >> print(a)}} > > gives you 120, but you cannot access it after the end of execution.Right. So learn to avoid depending on `print` for its side-effects: fac<-function(x){a<-1 for(i in 1:x){ a<-a*i } return(a)} fac(5) [1] 120> > > -- > View this message in context: http://r.789695.n4.nabble.com/Using-FUNCTION-to-create-usable-objects-tp4588681p4590549.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.David Winsemius, MD West Hartford, CT
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 <cel81009759 at gmail.com> 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 1:x){ >> ? ? ? ? ? ? ? ? a<-a*i >> ? ? ? ? ? ? ? ? print(a)}} > > gives you 120, but you cannot access it after the end of execution. > >-- Sarah Goslee http://www.functionaldiversity.org
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. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. michaelyb <cel81009759 at gmail.com> wrote:>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 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.
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 in 1:x){ a<-a*i } a} I did try the first one, but I got an error message, so I thought I was doing something wrong. However, once it finishes the loop, why the difference between the last lines? Best wishes! Mike -- View this message in context: http://r.789695.n4.nabble.com/Using-FUNCTION-to-create-usable-objects-tp4588681p4590774.html Sent from the R help mailing list archive at Nabble.com.
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: > > fac<-function(x){a<-1 > for (i in 1:x){ > a<-a*i > } > a}In R parsing, there is an implicit command separator performed by the end-of-line character. To get the first one to run without adding an end-of-line you can use a semi-colon: fac<-function(x){a<-1 for (i in 1:x){ a<-a*i }; a} Some people get annoyed when these are added unnecessarily, as may happen for SAS programmers who come over to R and are used to always using semicolons.> I did try the first one, but I got an error message, so I thought I > was > doing something wrong. However, once it finishes the loop, why the > difference between the last lines?-- David Winsemius, MD West Hartford, CT