R如何使用条件从dplyr中找到Interval或ntile

R How to findInterval or ntile from dplyr using conditional

我的目标是找到基于职业的模型连续响应的区间。 这是一个可重现的例子:

tbl = tibble(profession = c(rep('doctor', 50), rep('professor', 75), rep('student', 75)), response = rnorm(200))

我想实现的是这样的:

tbl <- tbl %>% group_by(profession) %>% mutate(rank = ntile(10))

谢谢你

dplyr 函数 ntile 需要两个参数,列名和 n.

library(dplyr)

tbl %>%
  group_by(profession) %>% 
  mutate(rank = ntile(response, n = 10))
## A tibble: 200 x 3
## Groups:   profession [3]
#   profession response  rank
#   <chr>         <dbl> <int>
# 1 doctor       0.278      7
# 2 doctor       0.586      8
# 3 doctor       0.0847     6
# 4 doctor       1.99      10
# 5 doctor       1.16       9
# 6 doctor       0.741      9
# 7 doctor      -1.19       2
# 8 doctor      -0.332      5
# 9 doctor       0.378      7
#10 doctor       0.649      8
## … with 190 more rows