0

In R, I have come across an example in which both = and <- are valid for assignment. e.g.

> y = c("hello", "world")
> y
[1] "hello" "world"
> y2 <- c("hello", "world")
> y2
[1] "hello" "world"

I have also come across an example in which = is invalid and <- is valid. e.g.

> quote(y[1] <- 1)
y[1] <- 1
> quote(x[1] = 1)
Error: unexpected '=' in "quote(x[1] ="

My question is, are there any cases in which the vice versa is true? i.e. <- is invalid whilst = is valid?

Reason for asking this question is to understand whether to stick with =, <-, or either (depending on circumstances) when performing assignment operation in R.

This will really help me setting up my mindset when performing coding in R.

Thank you!

Atlas7
  • 2,726
  • 4
  • 27
  • 36

2 Answers2

1

R has several assignment operators. Per the documentation

The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

The only place I am aware of where you must use the <- operator is naming items of a list in attach.

This does not work:

> attach(what <- list(foo <- function(x) print(x)))

but this does:

> attach(what <- list(foo = function(x) print(x)))

I don't actually know why this is. If anyone else knows I'd love to learn why.

I am also compelled to discourage use of any of the blasphemous right assignment operators.

Ellis Valentiner
  • 2,136
  • 3
  • 25
  • 36
  • `attach(what = list(foo = function(x) print(x)))` would be passing argument `what` with value `list(foo = function(x) print(x))` to `attach()`, which has a `what` argument. But the subtlety is that you never create an object `what`. The version using `<-` does: basically what you are doing is creating an object `what`, which is then attached as `what` is passed to the `what` argument. – Gavin Simpson Feb 26 '15 at 17:51
  • @GavinSimpson I take your point, but I don't see how this affects the assignment within the list. I'm not sure what it is about lists that prevents this from working: `attach(what = list(foo <- function(x) print(x)))` – Ellis Valentiner Feb 26 '15 at 18:54
  • And `what` ended up without names because of `<-` – Rich Scriven Feb 26 '15 at 19:15
  • Sorry, I misunderstood what you were showing. Why this `attach(what = list(foo <- function(x) print(x)))` fails is obvious. You are making the contents of a list available via their names and yet you've created a list without any names - run `list(foo <- function(x) print(x))` on it's own and you see this. – Gavin Simpson Feb 26 '15 at 19:37
  • @GavinSimpson of course! That makes a lot of sense. I can't believe I never realized this before. – Ellis Valentiner Feb 26 '15 at 20:01
  • Thank you @user12202013 for presenting an example in which `=` works and `<-` doesn't. And thank you everybody for helping out! I now understand `=` is for local argument assignment whereas `<-` is for global variable assignment, which I did not know before. – Atlas7 Mar 01 '15 at 20:42
1

Do not confuse yourself. Use <- when you assign a value to a variable, use = when setting a function's arguments. This, I believe, is the clearest way of doing things easily and normally and doing what you most probably want to do.

Assignment using <- as opposed to =

I'm defining a function

temp <- function(arg)
    2*arg

Let's call it:

temp(arg = 2)
[1] 4
temp(arg <- 2)
[1] 4

Seems it is the same, but is it?

Let us print arg:

arg ## first case
Error: object 'arg' not found
arg ## second case
[1] 2

Weird, isn't it? You most probably didn't want to store that value to that variable, but it happened, because you used <-.

codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
  • Thank you @naltipar. This is a great example of usage of "<-" vs "=". So temp(arg <- 2) assign 2 to arg globally to the entire R program. Whereas temp(arg = 2) assign 2 to arg locally within the temp() scope. (I didn't know that! ;) – Atlas7 Feb 26 '15 at 18:17
  • … or you could simply always use `=` and ignore that `<-` exists. – Konrad Rudolph Feb 13 '20 at 10:28