How to use pipeline operator %>%?

The pipeline operator %>% belong to the magrittr library, but from 2014 is included into the dplyr package, allowing us to concat multiple operations.

Provide a mechanism for chaining commands with a new operator,%>%.This operator will send a value, or the result of an expression, to the next function.

.

What does it mean to concatenate functions?

Let’s gonna check different examples about how to concat functions:

Example of Algorithms

Given a list, composed by a header(an element) and a tail(a list). A composition of functions could return the second element of the list:head(tail(list))

.

Example from daily life I: conversion of units.

If I know how many miles and hours I have been walking, but I want to decide what my average speed is in feet per second, what usually happens is to combine several functions to get my result. I know the number of feet in a mile, therefore, if x is in miles per unit of time, I have a function f (x) that converts it into feet per unit of time. I also have g (x) that converts the length per unit of hours to the length per unit of seconds (since I know there are 60 seconds in a minute). I can use the composition of functions to take my speed of miles per hour and convert it to feet per second; If I do this calculation often enough, it will be useful to memorize the composite function h (x) = g (f (x)).

.

Example from daily life I: every time you buy a sale item (with a discount).

When you are standing in the store trying to decide if you can afford the item, the first thing you calculate is the discount. For example, I want to buy this $ 20 shirt, and it’s on sale with a 15% discount. This means that the shirt is really 17 dollars. Now, you must calculate how much the shirt will cost after the sales tax (let’s say it’s 8%). The total cost of the shirt after discount and sales tax will be $ 18.36. This computation process can be expressed as a composite function. If f (x) = The price of the shirt after the discount and g (x) = The price after the sales tax, then, The function for the final cost of the shirt = g (f (x)).

.

Basically the idea behind the concatenation of functions, is given a data set, a function f1 is applied; and then, a second function f2 is applied to the result obtained, without limits on the number of times for the operation concat.

.

How can you concatenate functions?

After explaining how we use the concatenation of functions in real life, let’s see some examples of how to use the pipeline operator in R.

Example 1

Using starwars dataset

library(dplyr)
# Anidando funciones
filter(starwars,species == "Droid")

# Usando operador pipeline
starwars %>%
filter(species == "Droid")

# Anidando funciones
count(filter(starwars,hair_color == "black",eye_color == "brown"))

# Usando operador pipeline `%>%`
starwars %>% filter ((hair_color == "black"),(eye_color == "brown")) %>% count()

.

Example 2

Using the babynames dataset

library(babynames)
library(dplyr)

# Cuantas ninas con el nombre Victoria existen en el dataset
sum(select(filter(babynames,sex=="M",name=="Victoria"),n))

# Usando el operador pipeline `%>%`
babynames%>%
filter(sex=="F")%>%
filter(name=="Victoria")%>%
select(n)%>%
sum

In Github: A few of Examples.

.


What are the benefits of using the pipeline operator

The main reason is the simplicity it provides when building code, but if we want to explain a little more, we could say:

  • Simple and clear code
  • Less local variables
  • Simplicity at the moment to create the code: You’ll make it easy to add steps anywhere in the sequence of operations.

Sources