Simplify your code with the '|>' operator in RStudio

Published on
3 mins read
––– views

Introduction

Would you like to simplify your workflows in RStudio? Then you are in the right place! The |> operator is a powerful tool in the R base library that allows you to chain operations in a fluid and elegant way.

What is the pipe '|>' operator?

The |> operator (also known as "pipe forward") allows you to pass the result of one expression as the first argument of the next expression. This makes your code easier to read and understand.

We install the packages and load the libraries

To use the pipe operator in RStudio, you need to have R version 4.1.0 or higher installed. Additionally, you will need to load the R base library, which includes the pipe operator. To manipulate data, you can also load the dplyr library.

# Install and load the necessary libraries

install.packages("dplyr")

library(dplyr)

Example without the pipe operator

Load the data set

First, let's load the mtcars dataset. This data set contains measurements of 11 different attributes for 32 cars. We will use functions like filter, select, summary and create graphs to explore the data.

# Load the mtcars dataset
data(mtcars)

# View the first rows of the data set
head(mtcars)

Filter and select

Suppose we want to analyze only cars with a fuel consumption (mpg) greater than 20. We will filter and select the relevant columns:



# Filter cars with more than 150 horsepower
filtered_cars <- filter(mtcars, hp > 150)

# Select only the mpg, hp and wt columns
selected_columns <- select(filtered_cars, mpg, hp, wt)

# Calculate average miles per gallon (mpg), horsepower (hp) and weight (wt)
avg_mpg <- mean(selected_columns$mpg)
avg_hp <- mean(selected_columns$hp)
avg_wt <- mean(selected_columns$wt)

# Print the results
print(avg_mpg)
print(avg_hp)
print(avg_wt)

Repeating the same with the operator |>

Now, let's do the same using the |> operator:

library(dplyr)

# Load the mtcars dataset and perform chain operations using the pipe operator
result <- mtcars |>
  # Filter cars with more than 150 horsepower
  filter(hp > 150) |>
  # Select only the mpg, hp and wt columns
  select(mpg, hp, wt) |>
  # Calculate average miles per gallon (mpg), horsepower (hp) and weight (wt)
  summarise(avg_mpg = mean(mpg), avg_hp = mean(hp), avg_wt = mean(wt))

# Print the results
print(result)

Both examples achieve the same result, but the second example uses the pipe '|>' operator to chain the operations, making the code more concise and readable.

Now you try...