问题
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 ungroup
ing, 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