[R] [External Email] Pipe operator

Christopher Ryan cry@n @end|ng |rom b|ngh@mton@edu
Tue Jan 3 17:57:24 CET 2023


I think there are probably a number of purposes for (advantages to?)
the pipe operator. One is that it can avoid nested operations:

plot(mean(sqrt(c(1:10))))  ## this is my silly example code

which can get difficult to read.  This is arguably easier to read and
understand:

c(1:10) %>% sqrt() %>% mean() %>% plot()

As the chain of operations become longer, and as each "link" in the
chain becomes more complex, the value of the pipe approach, compared
to deep nesting in parentheses, increases, in my view.

--Chris Ryan

On Tue, Jan 3, 2023 at 11:48 AM Sorkin, John <jsorkin using som.umaryland.edu> 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 guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list