**Deeply Trivial**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

For the letter E, I want to talk about a set of operators provided by tidyverse (specifically the magrittr package) that makes for much prettier, easier-to-read code: pipes. The main pipe %>% pushes the object to the left of it forward into functions on the right, so that instead of coding f(x), it would be x %>% f(). This lets you chain together multiple functions to apply to a single object.

One of these pipes is known as the exposition pipe, and it looks like this: %$%. For many base R functions, there is no data attribute (i.e., data = …), and so when you specify one or more variables, you have to also specify the data frame (which would be formatted as data$variable). For instance, to run a correlation between two variables, you’d need to write the code as cor(data$variable1, data$variable2).

The exposition pipe exposes the columns of a data frame to the function, so you only need to specify the data frame once. This cleans up your code, while also allowing these functions to be embedded in a larger string of data wrangling and manipulation. Here’s how I can use the exposition code in action (but first, I’ve created a new version of the dataset that has a single Rating variable, which we created previously with the coalesce function, so you’ll want to download and use that file going forward):

library(tidyverse)

library(magrittr)

reads2019 <- read_csv("~/Downloads/Blogging A to Z/SarasReads2019_allrated.csv", col_names = TRUE)

reads2019 %$%

cor(MyRating, read_time)

## [1] -0.03762191

This is the correlation between my rating of the book and the number of days it took to read it. It’s a negative correlation, meaning the more I liked the book, the faster I got through it, which makes sense, but it’s also very weak. But what if I wanted to create a variable and then perform a correlation with that new variable? I can do that by combining the main pipe and the exposition pipe:

reads2019 %>%

mutate(DifRating = MyRating - AverageRating) %$%

cor(DifRating, read_time)

## [1] 0.007745212

The variable I created is the difference between my rating and the average rating from all Goodreads users. The correlation is basically 0, so there seems to be no relationship between how much more highly I rate a book (compared to others) and how quickly I finish it.

Tomorrow, we’ll talk about filters!

**leave a comment**for the author, please follow the link and comment on their blog:

**Deeply Trivial**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.