*********** Exercise = 3.15 ***********

Load DSLABS Library & Call the Murders Data :-

library(dslabs) 
data(murders)

*********** QUESTION = 1 ***********

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.
# Calculate the murder rate per 100,000 for each state
murder_rate <- (murders$murder / murders$population) * 100000

# Create a logical vector for rates lower than 1
low <- murder_rate < 1

*********** QUESTION = 2 ***********

2: Now use the results from the previous exercise and the function which to determine the indices of murder_rate associated with values lower than 1.
# Find the indices of rates lower than 1
indices_low <- which(low)

*********** QUESTION = 3 ***********

3: Use the results from the previous exercise to report the names of the states with murder rates lower than 1.
# Get the names of states with rates lower than 1
states_with_low_rates <- murders$state[indices_low]

*********** QUESTION = 4 ***********

4: Now extend the code from exercise 2 and 3 to report the states in the Northeast with murder rates lower than 1. Hint: use the previously defned logical vector low and the logical operator &.
# Define a logical vector for Northeast states
northeast <- murders$region == "Northeast"

# Find the states in the Northeast with rates lower than 1
northeast_low_rates <- murders$state[low & northeast]

*********** QUESTION = 5 ***********

5: In a previous exercise we computed the murder rate for each state and the average of these numbers. How many states are below the average?
# Compute the average murder rate for all states
average_murder_rate <- mean(murder_rate)

# Count the number of states with rates below the average
num_states_below_average <- sum(murder_rate < average_murder_rate)

*********** QUESTION = 6 ***********

6 : Use the match function to identify the states with abbreviations AK, MI, and IA. Hint: start by defning an index of the entries of murders$abb that match the three abbreviations, then use the [ operator to extract the states.
# Define a vector of abbreviations to match
abbreviations_to_match <- c("AK", "MI", "IA")

# Find the indices of the matching abbreviations
indices_match <- match(abbreviations_to_match, murders$abb)

# Extract the states using the indices
matching_states <- murders$state[indices_match]

*********** QUESTION = 7 ***********

7 : Use the %in% operator to create a logical vector that answers the question: which of the following are actual abbreviations: MA, ME, MI, MO, MU ?
# Define a vector of abbreviations to check
abbreviations_to_check <- c("MA", "ME", "MI", "MO", "MU")

# Create a logical vector to check if the abbreviations are in the dataset
actual_abbreviations <- abbreviations_to_check %in% murders$abb

*********** QUESTION = 8 ***********

8 : Extend the code you used in exercise 7 to report the one entry that is not an actual abbreviation. Hint: use the ! operator, which turns FALSE into TRUE and vice versa, then which to obtain an index.
# Use the ! operator to negate the logical vector
not_actual_abbreviation_index <- which(!actual_abbreviations)

# Find the non-abbreviation entry
non_abbreviation <- abbreviations_to_check[not_actual_abbreviation_index]