Nested if-else loops in R

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?

+4
source share
5 answers

, ifelse, case_when. /. @Marius , && &.

library(tidyverse)
crimes <- data.frame(pre_rate = c(0.27, 1.91, 2.81, 3.21, 4.80))

crimes %>% 
  mutate(rate_category = case_when(pre_rate > 0.26 & pre_rate < 0.87 ~ 1,
                                   pre_rate > 1.04 & pre_rate < 1.94 ~ 2,
                                   pre_rate > 2.03 & pre_rate < 2.96 ~ 3,
                                   pre_rate > 3.10 & pre_rate < 3.82 ~ 4,
                                   pre_rate > 4.20 & pre_rate < 11.00 ~ 5))
+4

, ? , pre_rate > num1 & pre_rate < num2 .

lowB <- c(0.26, 1.04, 2.03, 3.10, 4.2)
uppB <- c(0.87, 1.94, 2.96, 3.82, 11)

myCategory <- 1:5 ## this can be whatever categories you'd like

crimes$rate_category <- with(crimes, myCategory[pre_rate > lowB & pre_rate < uppB])
+1

ifelse()

# OP sample data set with one out-of-bounds value appended
crimes = data.frame(pre_rate = c(0.27, 1.91, 2.81, 3.21, 4.80, 1.0))   

library(data.table)
# specify categories, lower, and upper bounds
bounds <- data.table(
  cat = 1:5,
  lower = c(0.26, 1.04, 2.03, 3.10, 4.2),
  upper = c(0.87, 1.94, 2.96, 3.82, 11)
)
# non-equi join and update on join
setDT(crimes)[bounds, on = .(pre_rate > lower, pre_rate < upper), rate_category := cat][]
   pre_rate rate_category
1:     0.27             1
2:     1.91             2
3:     2.81             3
4:     3.21             4
5:     4.80             5
6:     1.00            NA

, pre-rate, , NA rate_category.

+1

, , ifelse:

pre_rate = c(0.27, 1.91, 2.81, 3.21, 4.80) 
crimes = data.frame(pre_rate)   
crimes$rate = (pre_rate > 0.26 & pre_rate < 0.87)*1 + 
  (pre_rate > 1.04 & pre_rate < 1.94)* 2 + 
  (pre_rate > 2.03 & pre_rate < 2.96)* 3 + 
  (pre_rate > 3.10 & pre_rate < 3.82)* 4 + 
  (pre_rate > 4.20 & pre_rate < 11.00)* 5

, , , . , NA -, , , . , , "&" , , ( ) , .

:

#> crimes
# pre_rate rate
#1     0.27    1
#2     1.91    2
#3     2.81    3
#4     3.21    4
#5     4.80    5
0

, .bincode:

crimes$rate_category <- .bincode(crimes$pre_rate,
                                 breaks = c(-Inf, 1, 2, 3, 4, Inf))

, data.table:

library(magrittr)
library(data.table)

rate_category_by_pre_rate <- 
  data.table(rate_category = c("foo", "bar", "foobar", "baz", "foobie"),
             pre_rate = c(1, 2, 3, 4, 11)) %>%
  setkey(pre_rate)

crimes %>%
  as.data.table %>%
  setkey(pre_rate) %>%
  rate_category_by_pre_rate[., roll = -Inf]

#>    rate_category pre_rate
#> 1:           foo     0.27
#> 2:           bar     1.91
#> 3:        foobar     2.81
#> 4:           baz     3.21
#> 5:        foobie     4.80

However, in your case, you may need to ceiling(i.e. round the value pre_rateand close it at 5:

crimes$rate_category <- pmin(ceiling(crimes$pre_rate), 5)

#>   pre_rate rate_category
#> 1     0.27             1
#> 2     1.91             2
#> 3     2.81             3
#> 4     3.21             4
#> 5     4.80             5
0
source

Source: https://habr.com/ru/post/1690214/


All Articles