I've been reading some code from an example in a blog post (here) and I came across an operator that I hadn't seen before. The author used a <<- operator to update a variable, like so:
ecov_xy <<- ecov_xy+decay*(x[t]*y[t]-ecov_xy) At first I thought it was a mistake and tried replacing it with the usual <- assignment operator, but I didn't get the same results. So what does the double arrow <<- operator do? |
I should probably point out that in the example, "ecov_xy " and "decay" are scalars, and x and y are vectors.
|
maybe
help ("<<-") helps daniel 2011-04-21 12:14 keltezéssel, Cliff Clive írta: > I should probably point out that in the example, "ecov_xy " and "decay" are > scalars, and x and y are vectors. > > > -- > View this message in context: http://r.789695.n4.nabble.com/What-does-the-operator-mean-tp3466657p3466672.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > > ______________________________________________ [hidden email] 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. |
In reply to this post by Cliff Clive
?assignOps
-Roy M. On Apr 21, 2011, at 12:08 PM, Cliff Clive wrote: > I've been reading some code from an example in a blog post ( > http://www.maxdama.com/ here ) and I came across an operator that I hadn't > seen before. The author used a <<- operator to update a variable, like so: > > ecov_xy <<- ecov_xy+decay*(x[t]*y[t]-ecov_xy) > > At first I thought it was a mistake and tried replacing it with the usual <- > assignment operator, but I didn't get the same results. So what does the > double arrow <<- operator do? > > -- > View this message in context: http://r.789695.n4.nabble.com/What-does-the-operator-mean-tp3466657p3466657.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. ********************** "The contents of this message do not reflect any position of the U.S. Government or NOAA." ********************** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center 1352 Lighthouse Avenue Pacific Grove, CA 93950-2097 e-mail: [hidden email] (Note new e-mail address) voice: (831)-648-9029 fax: (831)-648-8440 www: http://www.pfeg.noaa.gov/ "Old age and treachery will overcome youth and skill." "From those who have been given much, much will be expected" ______________________________________________ [hidden email] 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. |
In reply to this post by Cliff Clive
On Apr 21, 2011, at 2:08 PM, Cliff Clive wrote:
> I've been reading some code from an example in a blog post ( > http://www.maxdama.com/ here ) and I came across an operator that I hadn't > seen before. The author used a <<- operator to update a variable, like so: > > ecov_xy <<- ecov_xy+decay*(x[t]*y[t]-ecov_xy) > > At first I thought it was a mistake and tried replacing it with the usual <- > assignment operator, but I didn't get the same results. So what does the > double arrow <<- operator do? > It is a "super assignment" operator, which means that the assignment is done in the global environment, rather than to a variable that is within a function and therefore may only have local scope. This is covered a bit in "An Introduction To R": http://cran.r-project.org/doc/manuals/R-intro.html#Assignment-within-functions and a bit more in: ?"<-" or ?"<<-" It can be a bit hazardous to use, given the potential confusion over variable scoping issues, which is why <- and <<- may not be interchangeable as you are observing. HTH, Marc Schwartz ______________________________________________ [hidden email] 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. |
In reply to this post by Cliff Clive
On Fri, Apr 22, 2011 at 7:08 AM, Cliff Clive <[hidden email]> wrote:
> I've been reading some code from an example in a blog post ( > http://www.maxdama.com/ here ) and I came across an operator that I hadn't > seen before. The author used a <<- operator to update a variable, like so: > > ecov_xy <<- ecov_xy+decay*(x[t]*y[t]-ecov_xy) > > At first I thought it was a mistake and tried replacing it with the usual <- > assignment operator, but I didn't get the same results. So what does the > double arrow <<- operator do? It's the 'superassignment' operator. It does the assignment in the enclosing environment. That is, starting with the enclosing frame, it works its way up towards the global environment until it finds a variable called ecov_xy, and then assigns to it. If it never finds an existing ecov_xy it creates one in the global environment. The good use of superassignment is in conjuction with lexical scope, where an environment stores state for a function or set of functions that modify the state by using superassignment. make.accumulator<-function(){ a<-0 function(x) { a<<-a+x a } } > f<-make.accumulator() > f(1) [1] 1 > f(1) [1] 2 > f(11) [1] 13 > f(11) [1] 24 The Evil and Wrong use is to modify variables in the global environment. -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland ______________________________________________ [hidden email] 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. |
In reply to this post by Cliff Clive
On 22/04/11 07:08, Cliff Clive wrote:
> I've been reading some code from an example in a blog post ( > http://www.maxdama.com/ here ) and I came across an operator that I hadn't > seen before. The author used a<<- operator to update a variable, like so: > > ecov_xy<<- ecov_xy+decay*(x[t]*y[t]-ecov_xy) > > At first I thought it was a mistake and tried replacing it with the usual<- > assignment operator, but I didn't get the same results. So what does the > double arrow<<- operator do? Install the "fortunes" (if you haven't already) and see: fortune("<<-") cheers, Rolf Turner ______________________________________________ [hidden email] 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 Fri, Apr 22, 2011 at 9:39 AM, Rolf Turner <[hidden email]> wrote:
> On 22/04/11 07:08, Cliff Clive wrote: >> >> I've been reading some code from an example in a blog post ( >> http://www.maxdama.com/ here ) and I came across an operator that I hadn't >> seen before. The author used a<<- operator to update a variable, like so: >> >> ecov_xy<<- ecov_xy+decay*(x[t]*y[t]-ecov_xy) >> >> At first I thought it was a mistake and tried replacing it with the >> usual<- >> assignment operator, but I didn't get the same results. So what does the >> double arrow<<- operator do? > > Install the "fortunes" (if you haven't already) and see: > > fortune("<<-") > But note that in the quote Bill says "R/S" and it's dated 2001, so it's primarily based on experience with S. In S you don't have enclosing environments so only the Evil and Wrong uses of superassignment are possible. -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland ______________________________________________ [hidden email] 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. |
In reply to this post by Thomas Lumley-2
On Thu, Apr 21, 2011 at 9:59 PM, Thomas Lumley <[hidden email]> wrote:
> The Evil and Wrong use is to modify variables in the global environment. > I'm a bit at a loss. Why is it so wrong and evil? In other words, how should one modify variables in the global environment? Through the use of return()? Regards Liviu ______________________________________________ [hidden email] 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 22/04/11 17:53, Liviu Andronic wrote:
> On Thu, Apr 21, 2011 at 9:59 PM, Thomas Lumley<[hidden email]> wrote: >> The Evil and Wrong use is to modify variables in the global environment. >> > I'm a bit at a loss. Why is it so wrong and evil? Because there is extreme danger of accidentally overwriting objects in your workspace that you ***really*** wanted to keep! > In other words, how > should one modify variables in the global environment? Through the use > of return()? Roughly speaking yes, though ``return()'' is not strictly necessary. I.e. the returned value of a function may simply be the value of the last expression in the function. E.g. foo <- function(x) { 1 + x + x^2 } and foo <- function(x) { return(1 + x + x^2) } are effectively exactly the same function. Better to say via explicit assignment rather than ``through return()''. But basically functions should take input and produce output, where the latter may be assigned to an object the name of which is subject to your conscious choice. If you create the function: bar <- function(x) { y <<- 1 + x + x^2 invisible() } and do bar(3) you will wind up with an object named "y" in your workspace with the value 13. If you already had an object named "y" in your workspace (which may have taken hours or days of calculation) it would be gone, and you'd have to repeat those hours or days of calculation. Also the result of bar(x) is always named "y". Whereas with the function foo() previously defined you can do y <- foo(3) x <- foo(4) w <- foo(42) and have all three results available simultaneously to process further. Moreover you have to *consciously* overwrite an object named "y" by doing y <- foo(<whatever>). It can still happen of course, if you're not thinking carefully, but it's much less likely to happen. cheers, Rolf Turner ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |