Skip to main content
fix history based on comment
Source Link
Konrad Rudolph
  • 549.3k
  • 142
  • 967
  • 1.3k

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 originalAnother explanation by John Chambers, which the the R documentation is probably based on, actually explains this correctly:

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 original explanation by John Chambers, which the the R documentation is probably based on, actually explains this correctly:

The operator <- can be used anywhere, whereas the operator = is only allowed at the top level […] or as one of the subexpressions in a braced list of expressions.

Another explanation by John Chambers actually explains this correctly:

added 59 characters in body
Source Link
Konrad Rudolph
  • 549.3k
  • 142
  • 967
  • 1.3k
# # `x` does not exist: x # Error: object 'x' not found   sum((x = 1), 2) # [1] 3 # # Now, `x` exists: x # [1] 1 
x # Error: object 'x' not found sum((x = 1), 2) # [1] 3 x # [1] 1 
# # `x` does not exist: x # Error: object 'x' not found   sum((x = 1), 2) # [1] 3 # # Now, `x` exists: x # [1] 1 
Notice added Recommended answer in R Language by Sotos
added 35 characters in body
Source Link
Konrad Rudolph
  • 549.3k
  • 142
  • 967
  • 1.3k

Let’s not put too fine a point on it: the R documentation is (subtly) wrong [1]. This is easy to show: we just need to find a counter-example of the = operator that isn’t (a) at the top level, nor (b) a subexpression in a braced list of expressions (i.e. {…; …}). — Without further ado:

It’s because in R’s syntax the symbol = has two distinct meanings that get routinely conflated (even by experts, including in the documentation cited above):

  1. The first meaning is as an assignment operatorassignment operator. This is all we’ve talked about so far.
  2. The second meaning isn’t an operator but rather a syntax tokensyntax token that signals named argument passing in a function call. Unlike the = operatoroperator it performs no action at runtime, it merely changes the way an expression is parsed.

So how does R decide whether a given usage of = refers to the operator or to named argument passing? Let’s see.

A confession: I lied earlier. There is one additional difference betweenIn sum, by default the operators =<- and <-= operators: they call distinct functions. By default these functions do the same thing but you can override. But either of them can be overridden separately to change theits behaviour. By contrast, <- and -> (left-to-right assignment), though syntactically distinct, always call the same function. Overriding one also overrides the other. Knowing this is rarely practical but it can be used for some fun shenanigans.

Let’s not put too fine a point on it: the R documentation is (subtly) wrong [1]. This is easy to show: we just need to find a counter-example of the = operator that isn’t (a) at the top level, nor (b) a subexpression in a braced list of expressions (i.e. {…; …}). — Without further ado:

It’s because in R’s syntax the symbol = has two distinct meanings that get routinely conflated:

  1. The first meaning is as an assignment operator. This is all we’ve talked about so far.
  2. The second meaning isn’t an operator but rather a syntax token that signals named argument passing in a function call. Unlike the = operator it performs no action at runtime, it merely changes the way an expression is parsed.

Let’s see.

A confession: I lied earlier. There is one additional difference between the = and <- operators: they call distinct functions. By default these functions do the same thing but you can override either of them separately to change the behaviour. By contrast, <- and -> (left-to-right assignment), though syntactically distinct, always call the same function. Overriding one also overrides the other. Knowing this is rarely practical but it can be used for some fun shenanigans.

Let’s not put too fine a point on it: the R documentation is wrong. This is easy to show: we just need to find a counter-example of the = operator that isn’t (a) at the top level, nor (b) a subexpression in a braced list of expressions (i.e. {…; …}). — Without further ado:

It’s because in R’s syntax the symbol = has two distinct meanings that get routinely conflated (even by experts, including in the documentation cited above):

  1. The first meaning is as an assignment operator. This is all we’ve talked about so far.
  2. The second meaning isn’t an operator but rather a syntax token that signals named argument passing in a function call. Unlike the = operator it performs no action at runtime, it merely changes the way an expression is parsed.

So how does R decide whether a given usage of = refers to the operator or to named argument passing? Let’s see.

In sum, by default the operators <- and = do the same thing. But either of them can be overridden separately to change its behaviour. By contrast, <- and -> (left-to-right assignment), though syntactically distinct, always call the same function. Overriding one also overrides the other. Knowing this is rarely practical but it can be used for some fun shenanigans.

typo
Source Link
Kobi
  • 138.5k
  • 41
  • 259
  • 302
Loading
Source Link
Konrad Rudolph
  • 549.3k
  • 142
  • 967
  • 1.3k
Loading