-3

What is the difference between <-, << - and - >> in R programming?

I have tried to find the answer through Google and stackoverflow, but still could not get the answer.

Ferry T
  • 3
  • 1
  • 4
    Just take a look at the "R Assignment Operators" section of [this page](https://www.datamentor.io/r-programming/operator/), which was the top result when I copy and pasted your question into Google – duckmayr Jan 20 '20 at 19:58

1 Answers1

-1

<<- and <- are both assignment operators, but they are subtly different.

<- only applies to the local environment where it is used, so if you use it to assign a variable inside a function, that variable will not be available outside that function.

If you use <<- inside a function to declare a new variable with a name you haven't used anywhere else, it will create that variable in the global environment. If you use it to assign to an existing variable within your function (or any function which contains your function), it will be assigned to the existing variable instead.

It is almost always a bad idea to assign to the global environment from within a function. If you absolutely have to write variables from inside a function, it is better to use assign to write the variable to another persistent environment.

local_assign <- function() {a <- 1;}
global_assign <- function() {b <<- 1;}

local_assign()
global_assign()
a
# Error: object 'a' not found
b
# [1] 1
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • 4
    I think this leaves out important points about `<<-` and it's relation to the **parent** scope, not *necessarily* the global scope. I think the linked duplicates give a more complete overview of these operators. – MrFlick Jan 20 '20 at 20:42
  • 1
    Are you sure about that @MrFlick? What would you predict the outcome of the following would be? `f <- function(){g <- function(){b <<- 1}; g();}` If `<<-` only writes to the parent scope, then `b` should not exist globally when you call `f()`. – Allan Cameron Jan 20 '20 at 20:56
  • @MrFlick Of course, the other answers are more complete, and you are right to point this out, but I got the impression that the OP was a bit lost and needed a clear explanation without terms that might confuse them. Nevertheless, I genuinely appreciate the feedback. – Allan Cameron Jan 20 '20 at 20:59
  • 1
    Well, it will only will create one in the global if it doesn't exist in the parent. See for example `f <- function(){b<-5; g <- function(){b <<- 1}; g();}`. There will not be a `b` created in the global environment. – MrFlick Jan 20 '20 at 20:59
  • @MrFlick true enough. But what did you mean by not necessarily the global scope? What's the counterexample where `<<-` writes to a parent scope but not the global scope? Interested to learn – Allan Cameron Jan 20 '20 at 21:02
  • 4
    It changes values of variables in the parent scope (using lexical scoping, not the run time call stack) and will only write to global scope when the variables doesn't already exist. And I believe inside a package `<<-` will only write to the package namespace and not to the user global environment but i'd have to verify that. So the example I gave there is a `b<<-1` in the code but there is no `b` written to the global scope. `<<-` is the function to use when you are explicitly using closures. Otherwise `assign(, envir=.GlobalEnv)` is better to explicitly assign to global. – MrFlick Jan 20 '20 at 21:07
  • @MrFlick Thanks. I have just tested it by building a package, and `<<-` still writes to the global environment. So it seems the best way for me to describe it in simple terms to a beginner is that unless the variable name exists in the scope or one of the parent scopes, it will write to the global environment. I gave updated my answer. Thanks for your help. – Allan Cameron Jan 20 '20 at 21:26
  • 1
    Good to know. But in general answering duplicated questions doesn't much help the community. If you have a better answer than the ones provided there, then it's better to add your answer as an additional one there so we have a single place to redirect people for help. Question answers are asked to demonstrate "research effort." And if they can't find the dup because they might be using the wrong language or terms, we can help point them to the existing information. – MrFlick Jan 20 '20 at 21:30
  • @MrFlick I bow to your greater experience in this. My thought was that SO is also a great resource for beginners, but pointing them to an expert answer that requires knowledge of what a closure and an invocation is may leave them confused and disheartened when they wanted a simple answer. I accept that I may well be wrong about that. – Allan Cameron Jan 20 '20 at 21:40
  • 4
    Happy to have you here answering questions. That's an awesome thing to do. I just suggest focusing on questions that don't already have answers, or if they have confusing answers, add another, better answer. The site operates by up-voting the best answers to a question. It doesn't work as well when there are multiple copies of a question. – MrFlick Jan 20 '20 at 21:44