E is for Exposition Pipe
This article is originally published at http://www.deeplytrivial.com/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
reads2019 %>%
mutate(DifRating = MyRating - AverageRating) %$%
cor(DifRating, read_time)
## [1] 0.007745212
Tomorrow, we'll talk about filters!
Thanks for visiting r-craft.org
This article is originally published at http://www.deeplytrivial.com/
Please visit source website for post related comments.