Sort data in R data frame within subgroups

This question already has an answer here:

  • How to sort a dataframe by multiple column(s)? 16 answers

  • NEW UPDATE

    Much better now with that ISIN and more ties, I used two auxiliary columns. First, I generate the order by DATE, then group by the ISIN and get the min value for each group (that gives me the group order). My data.frame is named B.

    ord<-B %>% arrange(DATE) %>% mutate(ord=order(DATE))
    ord2<-ord %>% group_by(ISIN) %>% summarize(min_ord=min(ord))
    ord3<-merge(ord,ord2)
    ord3<-ord3 %>% arrange(min_ord)
    
    ISIN      CF       DATE ord min_ord
    1     E   5.000 2016-04-21   1       1
    2     E   5.000 2017-04-21   7       1
    3     E   5.000 2018-04-21   9       1
    4     E   5.000 2019-04-21  11       1
    5     E 105.000 2020-04-21  13       1
    6     B 104.875 2016-05-31   2       2
    7     F   7.800 2017-09-09   8       3
    8     F   7.800 2018-09-09  10       3
    9     F   7.800 2019-09-09  12       3
    10    F 107.800 2020-09-09  14       3
    11    F   7.800 2016-09-09   3       3
    12    A 105.750 2016-09-30   4       4
    13    D 103.875 2016-10-07   5       5
    14    C 106.875 2017-02-13   6       6
    

    You can delete the extra columns using select(ISIN:DATE) in the pipeline. I kept them because I thought they could be handy for extra calculations.

    OLD UPDATE

    Ok, the thing is your ISIN value is not working for the order you want to make. Sometimes, your ISIN goes in "descending" order (eg, 503326>255820>255817) but sometimes it doesn't and you want your DATE column to order your data.frame (eg, 2016-05-31 before 2016-09-30 before 2016-10-07 before 2017-02-13).

    Since in this case ISIN allows to use ifelse in a pseudo-convinient way:

    df %>% mutate(ord=ifelse(ISIN=="XS0503326083",1,
                      ifelse(ISIN=="XS0255820804",2,
                      ifelse(ISIN=="XS0255817685",3,
                      ifelse(ISIN=="XS0438753294",4,5))))) %>%
           arrange(ord)
    
      row.names         ISIN      CF       DATE ord
    1        16 XS0503326083   5.000 2016-04-21   1
    2        15 XS0503326083   5.000 2017-04-21   1
    3        14 XS0503326083   5.000 2018-04-21   1
    4        13 XS0503326083   5.000 2019-04-21   1
    5        12 XS0503326083 105.000 2020-04-21   1
    6         7 XS0255820804 104.875 2016-05-31   2
    7         6 XS0255817685 105.750 2016-09-30   3
    8        23 XS0438753294 103.875 2016-10-07   4
    9        22 XS0286431100 106.875 2017-02-13   5
    

    I know that you might have many ISIN value to incorporate to this kind of condition. Also, in your example the only tied ISIN values will get arranged by DATE and CF with no problems. This might not hold for your bigger data frame.


    data.table

    DT <- data.table(yourDF, key = c("ISIN", "Date"))
    

    library(dplyr)
    sorted <- df %>% arrange(ISIN,DATE)
    
    链接地址: http://www.djcxy.com/p/70858.html

    上一篇: 根据R中的多个列对数据框进行排序

    下一篇: 将子数据组中的R数据帧中的数据进行排序