R dplyr: How to filter murders dataset by region and homicide rate and select only specific columns without triggering validation errors
04:22 23 Nov 2025

I need some help in my coding related to Basic Data Wrangling. The instructions for coding in R are as follows:

Let's say you want to live in the Northeast or West in US and you want the homicide rate to be less than 1. We want to see data for states that satisfy these two conditions. Note that you can use logical operators with filter: filter(murders, population < 5000000 & region == "Northeast")

Instructions:

Add a homicide rate column and a rank column as we did above. Create a new data frame that satisfies both of the following conditions: the state is in the northeast or west and the homicide rate is less than 1. Call it my_states. Use select to display only the state name, rate, and rank.

Here is my code:

library(dslabs)
library(dplyr)

data(murders)

# Add homicide rate and rank columns
murders <- murders |>
  mutate(
    rate = total / population * 100000,
    rank = rank(-rate)
  )

# Filter for states in Northeast or West with homicide rate < 1
my_states <- murders |>
  filter(region %in% c("Northeast", "West") & rate < 1) |>
  select(state, rate, rank)

# View the result
my_states

I got two error messages: One is related to validating the object named 'my_states'. It says: Use the filter, %in% commands and the < operator to create a new table named my_states that satisfies both conditions. In the expected output, it shows abb, region, population, pop_rank, rate, and rank. But, in the instructions, it says explicitly to display only the state name, rate, and rank. When I add these variables, the code gives errors. The second error is in relation to validating the expected syntax.How to fix this problem? Thanks

r debugging data-wrangling