Tim, Thank you for your reply. I did not know about the |> operator. Do both %>% and |> work in base R? You suggested that the pipe operator can produce code with fewer variables. May I ask you to send a short example in which the pipe operator saves variables. Does said saving of variables speed up processing or result in less memory usage? Thank you, John ________________________________________ From: Ebert,Timothy Aaron <tebert at ufl.edu> Sent: Tuesday, January 3, 2023 12:07 PM To: Sorkin, John; 'R-help Mailing List' Subject: RE: Pipe operator The pipe shortens code and results in fewer variables because you do not have to save intermediate steps. Once you get used to the idea it is useful. Note that there is also the |> pipe that is part of base R. As far as I know it does the same thing as %>%, or at my level of programing I have not encountered a difference. Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Sorkin, John Sent: Tuesday, January 3, 2023 11:49 AM To: 'R-help Mailing List' <r-help at r-project.org> Subject: [R] Pipe operator [External Email] 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=7dDMSg%2FmPQ5xXP6zu6MWLmARdtdlrYWb3mXPZQj0La0%3D&reserved=0 PLEASE do read the posting guide https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=l5BZyjup%2Bho%2FijE1zQMxb5JE3F5VfKBZpUKHYW4k4Fg%3D&reserved=0 and provide commented, minimal, self-contained, reproducible code.
Christopher Ryan sent this example c(1:10) %>% sqrt() %>% mean() %>% plot() I could code this as A <- c(1:10) B <- sqrt(A) C<- mean(B) plot(C) I can then clean up by removing variables that I have no further use for. rm(A, B, C) The %>% operator is from the magriter package. It can be installed directly, or it is also installed if you use the tidyverse package (and possibly many others). The |> is base R, but it was added in R version 4.1.0. I do not know if it increases processing speed. It can save memory usage, especially if one is a messy programmer and does not tidy up after each task. If you wanted to test execution times for bits of code there is the microbenchmark package. Tim -----Original Message----- From: Sorkin, John <jsorkin at som.umaryland.edu> Sent: Tuesday, January 3, 2023 12:13 PM To: Ebert,Timothy Aaron <tebert at ufl.edu>; 'R-help Mailing List' <r-help at r-project.org> Subject: Re: Pipe operator [External Email] Tim, Thank you for your reply. I did not know about the |> operator. Do both %>% and |> work in base R? You suggested that the pipe operator can produce code with fewer variables. May I ask you to send a short example in which the pipe operator saves variables. Does said saving of variables speed up processing or result in less memory usage? Thank you, John ________________________________________ From: Ebert,Timothy Aaron <tebert at ufl.edu> Sent: Tuesday, January 3, 2023 12:07 PM To: Sorkin, John; 'R-help Mailing List' Subject: RE: Pipe operator The pipe shortens code and results in fewer variables because you do not have to save intermediate steps. Once you get used to the idea it is useful. Note that there is also the |> pipe that is part of base R. As far as I know it does the same thing as %>%, or at my level of programing I have not encountered a difference. Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Sorkin, John Sent: Tuesday, January 3, 2023 11:49 AM To: 'R-help Mailing List' <r-help at r-project.org> Subject: [R] Pipe operator [External Email] 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7Cfa39e74a28354e3b3f6c08daedadd2ab%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638083628073049849%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=qCK4H%2BtClknwzT9sQpQAUeei9I6dFz7vP904X0n39cw%3D&reserved=0 PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7Cfa39e74a28354e3b3f6c08daedadd2ab%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638083628073049849%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2F75VhIpUPDD1VjEHWJ5HBKcQO6cYciTJSMPJ9nETmMQ%3D&reserved=0 and provide commented, minimal, self-contained, reproducible code.
The other responses here have been very good, but I felt it necessary to point out that the concept of a pipe originated around when you started programming [1] (text based). It did take awhile for it to migrate into programming languages such as OCaml, but Powershell makes extensive use of (object-based) pipes. Re memory use: not so much. Variables are small... it is the data they point to that is large, and it is not possible to analyze data without storing it somewhere. But when the variables are numerous they can interfere with our ability to understand the program... using pipes lets us focus on results obtained after several steps so fewer intermediate values clutter the variable space. Re speed: the magrittr pipe (%>%) is much slower than the built-in pipe at coordinating the transfer of data from left to right, but that is not usually significant compared to the computation speed on the actual data in the functions. [1] https://en.m.wikipedia.org/wiki/Pipeline_(Unix)#:~:text=The%20concept%20of%20pipelines%20was,Ritchie%20%26%20Thompson%2C%201974). On January 3, 2023 9:13:22 AM PST, "Sorkin, John" <jsorkin at som.umaryland.edu> wrote:>Tim, > >Thank you for your reply. I did not know about the |> operator. Do both %>% and |> work in base R? > >You suggested that the pipe operator can produce code with fewer variables. May I ask you to send a short example in which the pipe operator saves variables. Does said saving of variables speed up processing or result in less memory usage? > >Thank you, >John > >________________________________________ >From: Ebert,Timothy Aaron <tebert at ufl.edu> >Sent: Tuesday, January 3, 2023 12:07 PM >To: Sorkin, John; 'R-help Mailing List' >Subject: RE: Pipe operator > >The pipe shortens code and results in fewer variables because you do not have to save intermediate steps. Once you get used to the idea it is useful. Note that there is also the |> pipe that is part of base R. As far as I know it does the same thing as %>%, or at my level of programing I have not encountered a difference. > >Tim > >-----Original Message----- >From: R-help <r-help-bounces at r-project.org> On Behalf Of Sorkin, John >Sent: Tuesday, January 3, 2023 11:49 AM >To: 'R-help Mailing List' <r-help at r-project.org> >Subject: [R] Pipe operator > >[External Email] > >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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=7dDMSg%2FmPQ5xXP6zu6MWLmARdtdlrYWb3mXPZQj0La0%3D&reserved=0 >PLEASE do read the posting guide https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=l5BZyjup%2Bho%2FijE1zQMxb5JE3F5VfKBZpUKHYW4k4Fg%3D&reserved=0 >and provide commented, minimal, self-contained, reproducible code. > >______________________________________________ >R-help at 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.-- Sent from my phone. Please excuse my brevity.
"Does saving of variables speed up processing" no "or save memory" no. The manual is quite explicit:> ?"|>"... Currently, pipe operations are implemented as syntax transformations. So an expression written as 'x |> f(y)' is parsed as 'f(x, y)'. Strictly speaking, using |> *doesn't* save any variables. x |> f(y) |> g() |> h(1,z) simply is h(g(f(x,y)),1,z) in which precisely the same variables appear. All that changes is the order in which you write the function names; the order in which things are evaluated does not change (the manual is explicit about that too). I personally find |> in R extremely confusing because in x |> f(y) |> g() |> h(1,z) it LOOKS as if there are calls to f(f), to g(), and to h(1,z) and in Haskell or F# that would be true, but in R the expressions f(y), g(), and h(1,z) are NOT evaluated. |> is and has to be special syntax with a very restricted right-hand side. Eliminating well-chosen variables can of course make code much less readable. It's funny how my code seems prettier using |> but other people's code seems hopelessly obscure... On Wed, 4 Jan 2023 at 06:19, Sorkin, John <jsorkin at som.umaryland.edu> wrote:> Tim, > > Thank you for your reply. I did not know about the |> operator. Do both > %>% and |> work in base R? > > You suggested that the pipe operator can produce code with fewer > variables. May I ask you to send a short example in which the pipe operator > saves variables. Does said saving of variables speed up processing or > result in less memory usage? > > Thank you, > John > > ________________________________________ > From: Ebert,Timothy Aaron <tebert at ufl.edu> > Sent: Tuesday, January 3, 2023 12:07 PM > To: Sorkin, John; 'R-help Mailing List' > Subject: RE: Pipe operator > > The pipe shortens code and results in fewer variables because you do not > have to save intermediate steps. Once you get used to the idea it is > useful. Note that there is also the |> pipe that is part of base R. As far > as I know it does the same thing as %>%, or at my level of programing I > have not encountered a difference. > > Tim > > -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Sorkin, John > Sent: Tuesday, January 3, 2023 11:49 AM > To: 'R-help Mailing List' <r-help at r-project.org> > Subject: [R] Pipe operator > > [External Email] > > 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=7dDMSg%2FmPQ5xXP6zu6MWLmARdtdlrYWb3mXPZQj0La0%3D&reserved=0 > PLEASE do read the posting guide > https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdc0d677272114cf6ba2808daedad0ec5%7C717009a620de461a88940312a395cac9%7C0%7C0%7C638083624783034240%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=l5BZyjup%2Bho%2FijE1zQMxb5JE3F5VfKBZpUKHYW4k4Fg%3D&reserved=0 > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]