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 %>%
This exercise is part of the course
Data Science R Basics
Exercise instructions
- 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.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load library
library(dplyr)
## Define the rate column
murders <- mutate(murders, rate = total / population * 100000, rank = rank(-rate))
# show the result and only include the state, rate, and rank columns, all in one line, in that order