filtering by two conditions
Suppose you want to live in the Northeast or West and want the murder rate to be less than 1. We want to see the data for the states satisfying these options. Note that you can use logical operators with filter
:
filter(murders, population < 5000000 & region == "Northeast")
This exercise is part of the course
Data Science R Basics
Exercise instructions
- Add a murder rate column and a rank column as done before
- Create a table, call it
my_states
, that satisfies both the conditions: it is in the Northeast or West and the murder rate is less than 1. - Use select to show only the state name, the rate and the rank
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# add the rate column
murders <- mutate(murders, rate = total / population * 100000, rank = rank(-rate))
# Create a table, call it my_states, that satisfies both the conditions
# Use select to show only the state name, the murder rate and the rank