How to Use The New Pipe in R 4.1

A simple new pipe expands readability options in R Programming

Pierre DeBois
Analytics Vidhya

--

Using pipes, such as the new native pipe in R, simplifies how functions appear in a code, easing the ability to intuitively debug a function

When a function is being created in a programming language, updates to that language can create new headaches in maintenance — a developer has to rewrite a section of code so that it is recognized. But in the case of R, a new base function is offering some simplicity in nesting lines of code.

R 4.1 introduced a pipe operator symbol, |>. It is the first time a pipe has been incorporated as a base function. The change is small, but the impact of simplifying code is significant.

Pipes are an operator meant to link one function after another. Pipes are set up with a symbol between the left side and right side of the expression. The pipes are read as “and then” in the line.

So in the image below, for example, line 20 is taking the first 8 rows in the dataframe mtcars, a built-in practice table of automobile specifications. So the line is read in the following way: assign mtcars to the object carslice “and then” apply the slice function to return rows 1 through 8.

Line 20 contains the pipe between the dataframe mtcars and the slice function

--

--

Pierre DeBois
Analytics Vidhya

#analytics |#datascience |#JS |#rstats |#marketing services for #smallbiz | #retail | #nonprofits Contrib @CMSWire @smallbiztrends #blackbusiness #BLM