1

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
user3022875
  • 8,598
  • 26
  • 103
  • 167
  • Do you need unique id for each group? https://stackoverflow.com/questions/36027431/how-to-give-numbers-to-each-group-of-a-dataframe-with-dplyrgroup-by – Ronak Shah Jul 19 '18 at 03:14

1 Answers1

0

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
akrun
  • 874,273
  • 37
  • 540
  • 662