Part 6 The %>%
forward-pipe operator
Let’s review the steps from the previous hands-on:
<- pivot_longer(df_w,
df_w2 cols=c("Weight_day0", "Weight_day10"),
values_to="weight",
names_to="day"
)<- separate(df_w2, col = "day",
df_w3 sep="_",
into=c(NA, "day")
)
If we are only interested in the final tidy data frame (and not in the intermediate object df_w2
), there is a simple and clean way to combine all steps.
The %>% (forward-pipe) operator from the magritrrr
package (the tidyverse
packages automatically load this operator):
The pipe allows to process the output of a function as the input of the following function: it makes the code easier to read and understand:
- It pipes the output of a function as the first argument of the next function (from left to right):
mytibble %>% function1
is equivalent tofunction1(mytibble)
mytibble %>% function2(y)
is equivalent tofunction1(mytibble, y)
mytibble %>% function1 %>% function2
is equivalent tofunction2(function1(mytibble))
If we go back to the previous exercise, we can simplify the code the following way:
<- pivot_longer(df_w,
df_w_clean cols=c("Weight_day0", "Weight_day10"),
values_to="weight",
names_to="day") %>%
separate(col = "day",
into=c(NA, "day"))
# This is equivalent to:
<- df_w %>% pivot_longer(cols=c("Weight_day0", "Weight_day10"),
df_w_clean values_to="weight",
names_to="day") %>%
separate(col = "day",
into=c(NA, "day"))
HANDS-ON
Simplify the following piece of code using the %>%
operator:
<- separate(data=table5, col=rate, into=c("cases", "population"), sep="/")
table5_1 <- unite(data=table5_1, col=year, c("century", "year"), sep="") table5_2
Answer
<- table5 %>%
table5_1 separate(col=rate, into=c("cases", "population"), sep="/") %>%
unite(col=year, c("century", "year"), sep="")