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.
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.
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. > > ______________________________________________ > 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. > >
?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. > > ______________________________________________ > 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.********************** "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: Roy.Mendelssohn at noaa.gov (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"
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
On Fri, Apr 22, 2011 at 7:08 AM, Cliff Clive <cliffclive at gmail.com> 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
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