Understanding Pipe Operators

R is constantly evolving towards more intuitive and readable ways for users to perform data manipulation. A powerful (newish) feature is the use of pipe operators: |> and %>%

Pip operators streamline the process of writing and understanding R code.

The Concept of Piping

Piping allows for a sequence of operations to be represented in a linear flow, like to a chain of commands. This approach makes code cleaner to read and write.

Let’s load tidyverse

# Install and load the tidyverse package if necessary
# install.packages("tidyverse")
library(tidyverse)

Introducing the |> Operator

The |> operator was introduced in R version 4.1.0 to offer a native solution for piping. The syntax is straightforward: the value or object on the left side of the operator is fed into the function on the right side as an argument.

Example of |>

# Suppose we have a simple numeric vector
numbers <- 1:10

# Using `|>` to apply a function
squared_numbers <- numbers |> sqrt() |> sum()
squared_numbers
[1] 22.46828

In this example, numbers is first passed into sqrt(), calculating the square root of each element, and the resulting vector is then passed into sum(), giving the total sum of square roots.

The %>% Operator

The %>% operator, provided by the magrittr package and by the tidyverse ecosystem, works similarly to |>, but adds flexibility for a vast array of data manipulation tasks.

More on the difference between the two here

For most operations |> will do the job. In doubt, use %>%

Example of %>%

This example would also works with |>.

setosa_df = iris %>% filter(Species == "setosa")

In this example, we filter the rows to keep only the setosa species.

The %>% operator makes chaining these operations straightforward and readable.

Conclusion

Pipe operators, |> and %>%, have significantly influenced the way R users approach data manipulation, allowing clearer, and more concise code.