I want to rank a column with ties, but not skip the next number if there are ties. currently when I do:
A B
group1 325
group1 325
group1 123
C <- df %>%
group_by(A) %>%
mutate(rank(-B, ties.method = "min")) %>%
ungroup()
I get
A B C
group1 325 1
group1 325 1
group1 123 3
Is there a way to make it not skip the number 2?
CodePudding user response:
We can use dense_rank
library(dplyr)
df %>%
group_by(A) %>%
mutate(C = dense_rank(-B)) %>%
ungroup
-output
# A tibble: 3 × 3
A B C
<chr> <int> <int>
1 group1 325 1
2 group1 325 1
3 group1 123 2
data
df <- structure(list(A = c("group1", "group1", "group1"), B = c(325L,
325L, 123L)), class = "data.frame", row.names = c(NA, -3L))