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,606
  • 2
  • 25
  • 34

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
  • 1,956
  • 3
  • 21
  • 34
  • `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 ` – 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 – 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 – 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 ` – 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,710
  • 2
  • 24
  • 37