1

What's the difference between "<-" and "<<-" in R? I have read the help file in R. But I still don't understand their difference. Could any one provide an example? Thanks a lot!

Alwang
  • 11
  • 1
  • 1
    From [@kohske's answer](http://stackoverflow.com/questions/9851655/why-is-using-frowned-upon-and-how-can-i-avoid-it/9851794#9851794): "<<- is NOT the operator to assign to global variable. It tries to assign the variable in the nearest parent environment" – talat Jul 25 '14 at 09:05
  • This is not the exact duplicate of @beginneR link, but this should be marked as one so the provided link will educate newbies how to properly use this operator – David Arenburg Jul 25 '14 at 09:12

1 Answers1

0

used inside a function, the standard "<-" operator defines a variable inside this function and the variable will not be available outside.

Using "<<-" enables your function to define a variable outside, aka in the global environment, for further use.

That is quite useful in fact!

agenis
  • 8,069
  • 5
  • 53
  • 102
  • 2
    Wrong. This should be never used to assign to the global environment. The only good use for it is via lexical scope – David Arenburg Jul 25 '14 at 09:13
  • @DavidArenburg I get it. When I use "<<-", R won't create a new variable but to look for the variable in parent environment. Am I right? – Alwang Jul 25 '14 at 09:42
  • 1
    It will first look in the parent environment and upwards, in case it won't find any, it will create a new one the global environment – David Arenburg Jul 25 '14 at 09:52