Start by loading the library and data.

library(dslabs)
data(murders)

Exercise

  1. Compute the per 100,000 murder rate for each state and store it in an object called murder_rate. Then use logical operators to create a logical vector named low that tells us which entries of murder_rate are lower than 1.

  2. Use the result from the previous exercise to report the names of the states with murder rates lower than 1. Store your result in an object called low_states.

  3. Now extend the code from exercises to report the states in the Northeast region and have a murder rate lower than 1. Store your result in an object called low_ne_states.

    Hint

    You will probably have to combine 2 logical vectors (each representing one condition) using the & operator (= and-operator). This will result in one logical vector that is TRUE where the corresponding elements in both vectors are TRUE
    e.g. c(TRUE, TRUE, FALSE) & c(TRUE, FALSE, TRUE) will result in c(TRUE, FALSE, FALSE)

  4. In a previous exercise we computed the murder rate for each state. How many states are below the average murder rate? Store your result in an object called states_below_avg.

    Hint

    You can use the sum function to count the number of TRUE logicals in a logical vector.
    e.g. sum(c(TRUE, TRUE, FALSE)) will return 2