Load the dplyr package and the murders dataset.
library(dplyr)
library(dslabs)
data(murders)
You can add columns using the dplyr function mutate
. This function
is aware of the column names and inside the function you can call them
unquoted:
murders <- mutate(murders, population_in_millions = population / 10^5)
We can write population
rather than murders$population
. The function
mutate
knows we are grabbing columns from murders
.
1. Use the function mutate
to add a murders column named rate
with the
per 100,000 murder rate as in the example code above. Make sure you
redefine murders
as done in the example code above (murders <- [your code]
) so we can keep using this variable.
2. If rank(x)
gives you the ranks of x
from lowest to highest,
rank(-x)
gives you the ranks from highest to lowest. Use the function
mutate
to add a column rank
containing the rank, from highest to
lowest murder rate. Make sure you redefine murders
so we can keep
using this variable.
3. With dplyr, we can use select
to show only certain columns.
For example, with this code we would only show the states and population
sizes:
select(murders, state, population)
Use select
to show the state names and abbreviations in murders
. Do
not redefine murders
and store this result in state_abb
.