Beginner’s Guide to Piping Data in R

Combine a variety of in-built functions with pipe operator to do powerful data analysis

Rishi Sidhu
The CodeHub

--

Photo by JJ Ying on Unsplash

The pipe operator %>% is used to pass the output of a function to another function, thereby enabling functions to be chained together. The end result is a block of very readable code with separate functions chained together.

Let’s see an example to understand. Consider the following 3 functions

  • Square of a number — square
  • Double of a number — double
  • Inverse of a number — inverse
  • Rounding off a number to 1 decimal place — round

Let’s say we want to apply these three functions to an array of numbers. One way to do that is to chain these functions together

#An array of numbers [0.1, 0.2, 0.3, ...., 1]
x = seq(0.1,1,by=0.1)
#Chaining Them Together
round(square(double(inverse(x)), digits=1)

For longer chains it is harder to read and difficult to keep track of parenthesis. Consider the alternative using pipes

#An array of numbers [0.1, 0.2, 0.3, ...., 1]
x <- seq(0.1,1,by=0.1)
#Pipeline of functions
x %>% square %>% double %>% inverse %>% round(digits=1)

--

--

Rishi Sidhu
The CodeHub

Blockchain | Machine Learning | Product Management