[Rd] order of operations

Enrico Schumann e@ @end|ng |rom enr|co@chum@nn@net
Fri Aug 27 21:06:54 CEST 2021


On Fri, 27 Aug 2021, Gabor Grothendieck writes:

> Are there any guarantees of whether x will equal 1 or 2 after this is run?
>
> (x <- 1) * (x <- 2)
> ## [1] 2
> x
> ## [1] 2

At least the "R Language Definition" [1] says

  "The exponentiation operator ‘^’ and the left
   assignment plus minus operators ‘<- - = <<-’
   group right to left, all other operators group
   left to right.  That is  [...]  1 - 1 - 1 is -1"

which would imply 2.

[1] https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Infix-and-prefix-operators

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



More information about the R-devel mailing list