组之间使用dplyr排名 [英] ranking with dplyr between groups

查看:44
本文介绍了组之间使用dplyr排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据框

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

,我想让fs的rank列都显示为1,而两个s的rank列都显示为2.基本上在range(-total_value)之后,我想添加一列,基于total_value列的组顺序为1,2、3等.

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

所以结果将是:

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

推荐答案

取消分组后,使用 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

这篇关于组之间使用dplyr排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆