Search code examples
rsortingpanel

Order values within column according to values within different column by group in R


I have the following panel data set:

group  i  f  r  d
1      4  8  3  3
1      9  4  5  1
1      2  2  2  2
2      5  5  3  2
2      3  9  3  3
2      9  1  3  1

I want to reorder column i in this data frame according to values in column d for each group. So the highest value for group 1 in column i should correspond to the highest value in column d. In the end my data.frame should look like this:

group  i  f  r  d
1      9  8  3  3
1      2  4  5  1
1      4  2  2  2
2      5  5  3  2
2      9  9  3  3
2      3  1  3  1

Solution

  • original (wrong)

    You can achieve this using dplyr and rank:

    library(dplyr)
    
    df1 %>% group_by(group) %>%
      mutate(i = i[rev(rank(d))])
    

    Edit

    This question is actually trickier than it first seems and the original answer I posted is incorrect. The correct solution orders by i before subsetting by the rank of d. This gives OP's desired output which my previous answer did not (not paying attention!)

    df1 %>% group_by(group) %>%
      mutate(i = i[order(i)][rank(d)])
    
    # A tibble: 6 x 5
    # Groups:   group [2]
    #  group     i     f     r     d
    #  <int> <int> <int> <int> <int>
    #1     1     9     8     3     3
    #2     1     2     4     5     1
    #3     1     4     2     2     2
    #4     2     5     5     3     2
    #5     2     9     9     3     3
    #6     2     3     1     3     1