R quantile by groups with assignments

自古美人都是妖i 提交于 2020-02-04 01:23:40

问题


I have the following df:

group = rep(seq(1,3),30)
variable = runif(90, 5.0, 7.5)
df = data.frame(group,variable)

I need to i) Define quantile by groups, ii) Assign each person to her quantile with respect to her group.

Thus, the output would look like:

id    group  variable  quantile_with_respect_to_the_group
1      1      6.430002     1
2      2      6.198008     3
          .......

There is a complicated way to do it with loops and cut function over each groups but it is not efficient at all. Does someone know a better solution ?

Thanks !


回答1:


In data.table:

library(data.table)

setDT(df)[,quantile := cut(variable, quantile(variable, probs = 0:4/4),
                         labels = FALSE, include.lowest = TRUE), by = group]

>head(df)
#    group variable quantile
# 1:     1 6.103909        2
# 2:     2 6.511485        3
# 3:     3 5.091684        1
# 4:     1 6.966461        4
# 5:     2 6.613441        4



回答2:


Another version with dplyr/findInterval

library(dplyr)
df %>%
  group_by(group) %>% 
  mutate(Quantile = findInterval(variable, 
                quantile(variable, probs=0:4/4)))


来源:https://stackoverflow.com/questions/35453942/r-quantile-by-groups-with-assignments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!