Reputation: 103
I am learning r and currently am looking at the pipe operator %>%, found in the magrittr
package. When I try the following:
pi/2 %>% sin
The result is 3.454967
, which is incorrect
Instead when I do:
(pi/2) %>% sin
The result is 1
, which is correct.
I am curious what is happening in the first case, from looking through the documentation here. I am unable to find anything useful. Could someone point me in the direction of some documentation for understanding this?
Upvotes: 0
Views: 37
Reputation: 206596
The order in which functions are evaualted depend on the operator precedence rules descrubed in the ?Syntax
help page
:: ::: 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 %%
functions have higher precedence then /
so they are run first. So essentially you are running
pi/(2 %>% sin)
# pi/0.9092974
# [1] 3.454967
Upvotes: 3