Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rank subgroup by group (dplyr)

Tags:

r

dplyr

This question addresses how to assign the rank of a row within a group. I would like to assign the rank of a subgroup to a row within that subgroup. What I'm really getting at is that I need an abbreviation of the second group_by variable that is guaranteed to be unique, and this is the best way I can think of to go about doing that. Hopefully the desired output below makes this clear enough.

Input dataframe:

my_df <- tibble(
  var1 = c(rep("A", 8), rep("B", 12)),
  var2 = c(rep("long_string_x", 4), 
           rep("long_string_y", 4),
           rep("long_string_x", 4), 
           rep("long_string_y", 4), 
           rep("long_string_z", 4))
)

Desired output:

# A tibble: 20 x 3
   var1  var2          group_rank
   <chr> <chr>              <dbl>
 1 A     long_string_x          1
 2 A     long_string_x          1
 3 A     long_string_x          1
 4 A     long_string_x          1
 5 A     long_string_y          2
 6 A     long_string_y          2
 7 A     long_string_y          2
 8 A     long_string_y          2
 9 B     long_string_x          1
10 B     long_string_x          1
11 B     long_string_x          1
12 B     long_string_x          1
13 B     long_string_y          2
14 B     long_string_y          2
15 B     long_string_y          2
16 B     long_string_y          2
17 B     long_string_z          3
18 B     long_string_z          3
19 B     long_string_z          3
20 B     long_string_z          3

How may I assign group_rank as above, ideally (but not necessarily) using a tidyverse approach?

like image 455
Rob Creel Avatar asked Dec 17 '22 11:12

Rob Creel


1 Answers

We could use match after grouping

library(dplyr)
my_df %>% 
   group_by(var1) %>%
   mutate(group_rank = match(var2, unique(var2))) %>%
   ungroup

-output

# A tibble: 20 x 3
   var1  var2          group_rank
   <chr> <chr>              <int>
 1 A     long_string_x          1
 2 A     long_string_x          1
 3 A     long_string_x          1
 4 A     long_string_x          1
 5 A     long_string_y          2
 6 A     long_string_y          2
 7 A     long_string_y          2
 8 A     long_string_y          2
 9 B     long_string_x          1
10 B     long_string_x          1
11 B     long_string_x          1
12 B     long_string_x          1
13 B     long_string_y          2
14 B     long_string_y          2
15 B     long_string_y          2
16 B     long_string_y          2
17 B     long_string_z          3
18 B     long_string_z          3
19 B     long_string_z          3
20 B     long_string_z          3
like image 67
akrun Avatar answered Jan 15 '23 12:01

akrun