What does the "<<-" operator mean?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

What does the "<<-" operator mean?

Cliff Clive
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?
Reply | Threaded
Open this post in threaded view
|

Re: What does the "<<-" operator mean?

Cliff Clive
I should probably point out that in the example, "ecov_xy " and "decay" are scalars, and x and y are vectors.
Reply | Threaded
Open this post in threaded view
|

Re: What does the "<<-" operator mean?

Kehl Dániel
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: What does the "<<-" operator mean?

Roy Mendelssohn - NOAA Federal
In reply to this post by Cliff Clive
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: What does the "<<-" operator mean?

Marc Schwartz
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.
Reply | Threaded
Open this post in threaded view
|

Re: What does the "<<-" operator mean?

Thomas Lumley-2
In reply to this post by Cliff Clive
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: What does the "<<-" operator mean?

Rolf Turner-3
In reply to this post by Cliff Clive
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: What does the "<<-" operator mean?

Thomas Lumley-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: What does the "<<-" operator mean?

Liviu Andronic
In reply to this post by Thomas Lumley-2
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: What does the "<<-" operator mean?

Rolf Turner-3
CONTENTS DELETED
The author has deleted this message.