This exercise builds upon previous exercise. Start by redefining murder to include rate and rank:
library(dplyr)
library(dslabs)
data(murders)
murders <- mutate(murders, rate=total/population*100000, rank=rank(-rate))
Suppose you want to live in a Northeast or West region and want the murder rate to be less than 1. Filter the murders
dataframe with these two conditions, store your result in a dataframe my_states
.
Hint
As always, there are multiple ways to tackle this problem:
- Use one filter function and combine the conditions with the
&
operator:
filter(dataframe, condition1 & condition2)
- Use the filter function with a parameters for each condition. The filter function will automatically combine these conditions with an
&
operator:
filter(dataframe,condition1, condition2)
- Filter on the first conditions and filter again on the second condition:
dataframe1 <- filter(dataframe, condition1)
dataframe2 <- filter(dataframe1, condition2)
Now use select
on my_states
to show only the state name (state
) and the population size (population
). Store the result in my_states_subset
.