John E.
John E.

Reputation: 137

R How to findInterval or ntile from dplyr using conditional

My goal is to find the interval of a model continuous response that falls in based on the profession. Here is a reproducible example:

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

What I want to achieve is something like that:

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

Thanks you

Upvotes: 1

Views: 284

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76460

dplyr function ntile needs two arguments, the column name and 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

Upvotes: 1

Related Questions