Nested if-else loops in R

前端 未结 5 1832
一生所求
一生所求 2021-01-19 03:28

I have a data frame named \"crimes\" which contains a \"pre_rate\" column that denotes the crime rate before a certain law is implemented. I would like to put each rate in a

5条回答
  •  再見小時候
    2021-01-19 04:13

    If your data does not contain gaps, and you just want an index, you can use .bincode:

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

    If you want specific values for each interval, you can use a rolling join via the data.table package:

    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 only need ceiling (i.e. round-up the value of pre_rate and cap 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
    

提交回复
热议问题