1. Learn
  2. /
  3. Courses
  4. /
  5. Data Science R Basics

Exercise

Using the pipe %>%

The pipe %>% can be used to perform operations sequentially without having to define intermediate objects. After redefining murder to include rate and rank.

library(dplyr)
murders <- mutate(murders, rate =  total / population * 100000, rank = (-rate))

in the solution to the previous exercise we did the following:

# Created a table 
my_states <- filter(murders, region %in% c("Northeast", "West") & rate < 1)

# Used select to show only the state name, the murder rate and the rank
select(my_states, state, rate, rank)

The pipe %>% permits us to perform both operation sequentially and without having to define an intermediate variable my_states

For example we could have mutated and selected in the same line like this:

mutate(murders, rate =  total / population * 100000, rank = (-rate)) %>% 
    select(state, rate, rank)

Note that select no longer has a data frame as the first argument. The first argument is assumed to be the result of the operation conducted right before the %>%

Instructions

100 XP
  • Repeat the previous exercise, but now instead of creating a new object, show the result and only include the state, rate, and rank columns in that order.
  • Use a pipe %>% to do this in just one line.