组之间使用dplyr排名

时间:2018-07-19 02:51:53

标签: r 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,而两个ES的rank列都显示为2。基本上在ranging(-total_value)之后,我想添加一列,该列是基于total_value列的组顺序1、2、3等。

所以结果将是:

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 个答案:

答案 0 :(得分:0)

ungroup之后,使用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