5.6 Pipes

Notice how to perform those actions, we had to create multiple objects. There is also some redundancy in the code because we had to name the dataframe each time. Wouldn’t it be great if we could chain together multiple actions? This is where the pipe comes in. Pipes let you take the output of one function and send it directly to the next, which is useful whenyou need to do many things to the same dataset. Pipes in R look like %>% and are made available via the magrittr package, installed automatically with dplyr. If you use RStudio, you can type the pipe with Ctrl + Shift + M if you have a PC or Cmd + Shift + M if you have a Mac.

Let’s do those previous three steps again, but this time using the pipe. We start with the name of the dataframe measles_us_modified and pipe it to the first function filter(). Since we already specified the data we do not need to include that in the arguments of filter(). Then we take the results of that, which we previously called non_fatal and pipe it to group_by(), and finally pipe the results of that function, which we had called non_fatal_by_group, into summarize(). This produces are final dataframe non_fatal_by_period.

#Use the pipe to chain those together
non_cumulative_period <- measles_us_modified %>%
  filter(PartOfCumulativeCountSeries == 0) %>%
  group_by(State, PeriodStartDate) %>%
  summarise(TotalCount = sum(CountValue))
## `summarise()` has grouped output by 'State'. You can override using the `.groups` argument.