Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of operation with piping

Tags:

r

magrittr

My question is where does the piping operator of magrittr package %>% come in the order of operations?

I have a problem simmilar to the following:

set.seed(10)
df <- data.frame(a=rnorm(3),b=rnorm(3),c=rnorm(3))
df/rowSums(df)  %>%  round(.,3) 

This results in the following non rounded figures:

           a        b         c
1 -0.0121966 0.119878 0.8922125

To get the rounded figures I need to put df/rowSums(df) between brackets.

I experimented with the +,-,*,/ and ^ and from the results I found the order of operation is as follow:

  1. Exponents
  2. Piping
  3. Multiplication and division
  4. Addition and subtraction

Is that right or there is something wrong with my understanding of the piping operator?

like image 780
mallet Avatar asked Jul 22 '16 16:07

mallet


People also ask

What package is %>% in in R?

%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).

What is pipe operation R?

What is the Pipe Operator? The pipe operator is a special operational function available under the magrittr and dplyr package (basically developed under magrittr), which allows us to pass the result of one function/argument to the other one in sequence. It is generally denoted by symbol %>% in R Programming.

What does Magrittr do in R?

The magrittr package offers a set of operators which make your code more readable by: structuring sequences of data operations left-to-right (as opposed to from the inside and out), avoiding nested function calls, minimizing the need for local variables and function definitions, and.


1 Answers

The help page you are looking for is ?Syntax. (Don't feel bad for not being able to find this, it took me about six guesses at search keywords.) I'm going to quote its entire operator precedence table here:

The following unary and binary operators are defined. They are listed in precedence groups, from highest to lowest.

   ‘:: :::’           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)                      

So magrittr's pipe operators, like all the operators of the form %whatever%, do indeed have precedence greater than multiply and divide but lower than exponentiation, and this is guaranteed by the language specification.


Personally, I don't see the value in these operators. Why not just write

round(df/rowSums(df), 3)

which has the evaluation order you want, and is (IMNSHO) easier to read as well?

like image 63
zwol Avatar answered Sep 24 '22 05:09

zwol