[R] Pipe operator

Ivan Calandra |v@n@c@|@ndr@ @end|ng |rom |e|z@@de
Tue Jan 3 17:58:56 CET 2023


Dear John,

some more experienced users might give you a different and more helpful 
answer, but I was not really convinced by the pipe operator until I 
tried it out, for the same reasons as you.

In my opinion, the pipe operator is there only to improve the 
readability of your code. Think about e.g. format()ing or round()ing the 
example you gave: you start having a lot of imbricated functions and it 
becomes difficult to read (because of lots of brackets, commas and so 
on, and it gets worse when adding arguments). The pipe operator makes it 
clearer.
An alternative to the pipe operator with good readability is creating 
intermediary objects, but you create a lot of useless objects. Depending 
on the size of the objects, it could become problematic.

Somehow, I just ended up paraphrasing Wickham & Grolemund 
(https://r4ds.had.co.nz/pipes.html); they explain the advantages much 
better than I can.

In any case, once I started using it, I realized that all the pros for 
the pipe operator are real and now I like using it!

Best,
Ivan




	*LEIBNIZ-ZENTRUM*
*FÜR ARCHÄOLOGIE*

*Dr. Ivan CALANDRA*
**Imaging Lab

MONREPOS Archaeological Research Centre, Schloss Monrepos
56567 Neuwied, Germany

T: +49 2631 9772 243
T: +49 6131 8885 543
ivan.calandra using leiza.de

leiza.de <http://www.leiza.de/>
<http://www.leiza.de/>
ORCID <https://orcid.org/0000-0003-3816-6359>
ResearchGate
<https://www.researchgate.net/profile/Ivan_Calandra>

LEIZA is a foundation under public law of the State of 
Rhineland-Palatinate and the City of Mainz. Its headquarters are in 
Mainz. Supervision is carried out by the Ministry of Science and Health 
of the State of Rhineland-Palatinate. LEIZA is a research museum of the 
Leibniz Association.

On 03/01/2023 17:48, Sorkin, John wrote:
> I am trying to understand the reason for existence of the pipe operator, %>%, and when one should use it. It is my understanding that the operator sends the file to the left of the operator to the function immediately to the right of the operator:
>
> c(1:10) %>% mean results in a value of 5.5 which is exactly the same as the result one obtains using the mean function directly, viz. mean(c(1:10)). What is the reason for having two syntactically different but semantically identical ways to call a function? Is one more efficient than the other? Does one use less memory than the other?
>
> P.S. Please forgive what might seem to be a question with an obvious answer. I am a programmer dinosaur. I have been programming for more than 50 years. When I started programming in the 1960s the only pipe one spoke about was a bong.
>
> John
>
> ______________________________________________
> R-help using r-project.org  mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list