Order of operations in the R

Asked

Viewed 206 times

3

How does the order of operations in R work? And how can I change them?

For example, when I do 1 + 2^2 in the R, he knows that first it is necessary to evaluate the 2^2 and then add the result to the 1.

In this case, of course it makes no sense to want to change this order. But, what I would like to do is something like the reverse of the operator %>% of dplyrthat works as follows:

exp %<% 1 + 1
# 7.389056 = exp(2)

Thus, it would be necessary that the 1+1was evaluated before the %<%, is this possible?

I tried that:

"%<%" <- function(l,r) return(l(r))
exp %<% 1 + 1
# 3.718282

But that’s the same thing:

exp(1) + 1
# 3.718282
  • You don’t want to do exp %<% (1+1), right?

  • exact, did not want to use parentheses

  • I do not know if there is how to do this, even because it would be a mess if this was something customizable, making it very difficult to understand/read codes, besides causing others to stop working as expected.

  • I agree, it can create confusion. But there are other functions that do this kind of thing... For example the operator <-, when I do x <- 1 + 1, 1 + 1 is calculated before <- be actually used.

  • Daniel, what did you say has to do with ? Syntax (https://stat.ethz.ch/R-manual/R-devel/library/base/html/Syntax.html), showing the precedence of all operators. Note that %any% is one of them. That is, when defining an operator %<% he has less precedence than +, but greater than * (note that in your example exp %<% 2 * 2 gives the result you wanted). The problem (problem) is trying to change the precedence of your function. I think if it is possible this task is difficult (you will need to define classes), but I can be wrong. Veja ? groupGeneric e ? S4groupgeneric

  • Vixe, I said nonsense. It also does not work with *, for the precedence of %any% is bigger. But it works with :, for example. exp %<% 1:10 results in a vector with the results. Or exp %<% 2^2, who also gives you what you wanted.

  • @Juliotrecenti is that, like + (binary) takes precedence to <-, then in x <- 1 + 1 the sum is made before assigning. In this case, Daniel’s operator %<% is a special operator who takes precedence over + (binary), then the %<% will run first unless parentheses are placed. I think you should put this as a response.

  • Daniel, +1 good question. I don’t think you can change the precedence of a binary operator for the reasons @Rcoster listed.

  • @Carloscinelli I didn’t put as an answer because I don’t have a solution and I can’t prove that it’s impossible (or extremely difficult) to make this change of precedence, so it’s not really an answer. Makes sense? I still don’t know how to use the OS right.

  • 1

    @Juliotrecenti Yes, no problem, Daniel asked two things "Como funciona a ordem das operações no R? E como posso alterá-las?" you can explain the order of operations, then you will be answering a part of the question, and when the second part has no problem to say that you do not know.

  • 1

    @Carloscinelli +1 for the tip. Convinced me, thank you.

Show 6 more comments

1 answer

2


I will rewrite part of my comments as an answer to the question

How the order of operations in R works?

@Danielfalbel, what you said has to do with the concept of "precedence" and is defined in ?Syntax, which shows the precedence of all R operators. Below I copy the R help text, showing the hierarchy of operators (the first is the one with the highest precedence):

:: :::  access variables in a namespace
$ @ component / slot extraction
[ [[    indexing
^   exponentiation (right to left)
- + unary minus and plus
:   sequence operator
%any%   special operators (including %% and %/%)
* / multiply, divide
+ - (binary) add, subtract
< > <= >= == != ordering and comparison
!   negation
& &&    and
| ||    or
~   as in formulae
-> ->>  rightwards assignment
<- <<-  assignment (right to left)
=   assignment (right to left)
?   help (unary and binary)

Note that %any% (that is, any operator defined as % %) is one of them. That is, when defining an operator %<% he has greater precedence than +, but less than ^ (note that in your example exp %<% 2 ^ 2 gives the result you wanted).

The problem (problem) is trying to change the precedence of your function. A trivial solution would be to use parentheses,

exp %<% (1+1)

but from what you said you wanted to avoid them.

Taking a look at the operator definitions, I think that if possible this task can be quite difficult (you will probably need to define classes that work alongside primitive operators explicitly), but I could be wrong. Behold ?groupGeneric and ?S4groupgeneric for some details on how primitive operators are implemented.

  • "when defining an operator %<% it takes precedence less than +, but greater than " would be the opposite, the %<% takes precedence greater than + (binary, summation) and less than ^.

  • @Carloscinelli corrected.

  • very cool Julio! it really seems to be tricky trying to change that order... maybe it is possible to make some gambiarra using non-standard Evaluation and sys.call

Browser other questions tagged

You are not signed in. Login or sign up in order to post.