library(magrittr)
library(dplyr)
library(tidyr)
library(DT)
library(data.table)Magrittr is the source of pipe operators in R. Although the most commonly used pipe operator, %>% is now available in dplyr, it also has other interesting pipe options.
To begin testing Magrittr, we import a Heart Disease dataset exported from Kaggle.
heart <- read.csv("https://raw.githubusercontent.com/mkivenson/Data-Acquisition-and-Management/master/TidyVerse/heart.csv")
heart = rename(heart,age = ï..age)
heart2 = heart
datatable(heart)Let’s say that we want to perform the following data wrangling techniques on the dataset:
Using dpylr, tidyr, and data.table alone (without piping operators), this code would look something like this:
data <- select(heart, age, sex, trestbps, chol)
data <- filter(data, (age <= 50) & (sex == 0))
data <- gather(data, "metric", "value", 3:4)
data <- arrange(data, age)
datatable(data)If we instead use the magrittr %>% pipe operator, we can do this. It is the same result but somewhat cleaner.
data <- heart %>%
select(age, sex, trestbps, chol) %>%
filter((age <= 50) & (sex == 0)) %>%
gather("metric", "value", 3:4) %>%
arrange(age)
datatable(data)What if we wanted to modify the mtcars dataframe directly, rather than creating a new dataframe (or writing heart <- heart)? We can just use the %<>% operator instead of %>%:
heart %<>%
select(age, sex, trestbps, chol) %>%
filter((age <= 50) & (sex == 0)) %>%
gather("metric", "value", 3:4) %>%
arrange(age)
datatable(heart)Other magrittr operators include the %T>% and %$% pipes
One interesting feature of the magrittr package is its ability to create unary functions with pipe operators. Unary functions are functions that accept one input and apply piping operators to the input. Essentially, unary functions allow users to create a function for a complicated or long piping process and condense it into one function call. This can be useful if a user is performing the same operations multiple times on similarly structured data.
Using the example presented about the %<>% operator, I can condense the entire operation into one easy function using “.”.
pipeassign = . %>%
select(age, sex, trestbps, chol) %>%
filter((age <= 50) & (sex == 0)) %>%
gather("metric", "value", 3:4) %>%
arrange(age) %>%
datatable()
pipeassign(heart2)