I have a data frame called “crimes” that contains a “pre_rate” column that indicates the level of crime before a particular law is implemented. I would like to put each bet in the "rate_category" column using an if-else nested loop. I have the following code:
crimes$rate_category =
with(crimes, ifelse(pre_rate > 0.26 && pre_rate < 0.87, 1,
ifelse(pre_rate > 1.04 && pre_rate < 1.94, 2,
ifelse(pre_rate > 2.03 && pre_rate < 2.96, 3,
ifelse(pre_rate > 3.10 && pre_rate < 3.82, 4,
ifelse(pre_rate > 4.20 && pre_rate < 11.00, 5, "NA"))))))
crimes
and here is a reproducible example:
pre_rate = c(0.27, 1.91, 2.81, 3.21, 4.80)
crimes = data.frame(pre_rate)
crimes
However, when I run the loop with my original data frame, all the levels in the "rate_category" column are incorrectly set to 1. What, apparently, is the problem with the loop above?
source
share