ranking with dplyr between groups

百般思念 提交于 2021-02-05 07:58:25

问题


I have this dataframe

library(dplyr)
d =data.frame(group1 = c("A","B","A","B"), group2 = c("e","f","e","f"), value=c(1,2,3,4) )

d%>% group_by(group2) %>% mutate(total_value = sum(value)) %>% arrange(-total_value) %>% mutate( rank =  rank(-total_value, ties.method = "max") )


group1 group2 value total_value  rank
  <fct>  <fct>  <dbl>       <dbl> <int>
1 B      f          2           6     2
2 B      f          4           6     2
3 A      e          1           4     2
4 A      e          3           4     2

and I'd like to have the rank column show 1 for both fs and 2 for boths es. Basically after the arrange(-total_value) I'd like a add a column that is the group order 1,2, 3 etc.... based on the total_value column

so the results would be:

group1 group2 value total_value  some_new_column
  <fct>  <fct>  <dbl>       <dbl> <int>
1 B      f          2           6     1
2 B      f          4           6     1
3 A      e          1           4     2
4 A      e          3           4     2

回答1:


After ungrouping, use dense_rank

d %>% 
   group_by(group2) %>%
   mutate(total_value = sum(value)) %>% 
   arrange(-total_value) %>%
   ungroup %>% 
   mutate( rank =  dense_rank(-total_value) )
# A tibble: 4 x 5
#  group1 group2 value total_value  rank
#  <fct>  <fct>  <dbl>       <dbl> <int>
#1 B      f          2           6     1
#2 B      f          4           6     1
#3 A      e          1           4     2
#4 A      e          3           4     2


来源:https://stackoverflow.com/questions/51413641/ranking-with-dplyr-between-groups

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