代码之家  ›  专栏  ›  技术社区  ›  Varun

基于数据帧中的变量组对行进行排序[重复]

  •  0
  • Varun  · 技术社区  · 6 年前

    df=data.frame(Code=c('Q1','Q1','Q1','Q1','Q2','Q2','Q2','Q2','Q3','Q3','Q3','Q3'),
              Fiscal_Year=c('FY18','FY16','FY17','FY15','FY15','FY18','FY17','FY16','FY15','FY16','FY17','FY18'),
              Score=c(0.23,0.25,0.32,0.41,0.61,0.54,0.45,0.51,0.78,0.79,0.81,0.84))
    

    我的目标是按问题分组 Code (在dataframe中已经是这样了),然后对每个组的行按 Fiscal_Year .

    因此水平顺序为FY15<2016财年<2017财年<2018财年。

    为了定义R中的级别,我做了以下操作-

    #Convert column to factor
    df$Fiscal_Year=as.factor(df$Fiscal_Year)
    #Define order of levels
    levels(df$Fiscal_Year)=c("FY15","FY16","FY17","FY18")
    

    接下来,我要分组 代码 然后 按递增顺序排序 属于 会计年度 每组内 .

    例如,对于问题组 Q2 ,我希望按财政年度(FY15、FY16、FY17、FY18)的递增顺序对行进行排序。其他问题组也一样。

    我的尝试

    library(dplyr)
    
    df=sat %>%
    group_by(CTQ_QUEST_CODE,FY) %>%
    arrange(CTQ_QUEST_CODE,desc(FY))
    

    但是我没有得到我想要的结果。

    如果您对此有任何意见,我们将不胜感激。

    4 回复  |  直到 6 年前
        1
  •  4
  •   nghauran    6 年前

    只需添加 .by_group = TRUE arrange() :

    df %>%
            group_by(Code) %>%
            arrange(Fiscal_Year, .by_group = TRUE)
    
        2
  •  2
  •   Ronak Shah    6 年前

    我们可以 arrange 通过 Code 和数字部分 Fiscal_Year

    library(dplyr)
    library(readr)
    
    df %>%
        arrange(Code, parse_number(Fiscal_Year))
    
    #   Code Fiscal_Year Score
    #1    Q1        FY15  0.41
    #2    Q1        FY16  0.25
    #3    Q1        FY17  0.32
    #4    Q1        FY18  0.23
    #5    Q2        FY15  0.61
    #6    Q2        FY16  0.51
    #7    Q2        FY17  0.45
    #8    Q2        FY18  0.54
    #9    Q3        FY15  0.78
    #10   Q3        FY16  0.79
    #11   Q3        FY17  0.81
    #12   Q3        FY18  0.84
    
        3
  •  1
  •   BENY    6 年前

    从R基 order

    df[order(df$Code,df$Fiscal_Year),]
       Code Fiscal_Year Score
    4    Q1        FY15  0.41
    2    Q1        FY16  0.25
    3    Q1        FY17  0.32
    1    Q1        FY18  0.23
    5    Q2        FY15  0.61
    8    Q2        FY16  0.51
    7    Q2        FY17  0.45
    6    Q2        FY18  0.54
    9    Q3        FY15  0.78
    10   Q3        FY16  0.79
    11   Q3        FY17  0.81
    12   Q3        FY18  0.84
    
        4
  •  0
  •   jvent    6 年前

    desc() arrange()

    > df %>%
    + group_by(Code,Fiscal_Year) %>%
    + arrange(Code,Fiscal_Year)
    
    # A tibble: 12 x 3
    # Groups:   Code, Fiscal_Year [12]
    
       Code  Fiscal_Year Score
       <fct> <fct>       <dbl>
     1 Q1    FY15         0.41
     2 Q1    FY16         0.25
     3 Q1    FY17         0.32
     4 Q1    FY18         0.23
     5 Q2    FY15         0.61
     6 Q2    FY16         0.51
     7 Q2    FY17         0.45
     8 Q2    FY18         0.54
     9 Q3    FY15         0.78
    10 Q3    FY16         0.79
    11 Q3    FY17         0.81
    12 Q3    FY18         0.84
    
    > df %>% arrange(Code, Fiscal_Year)
    
       Code Fiscal_Year Score
    1    Q1        FY15  0.41
    2    Q1        FY16  0.25
    3    Q1        FY17  0.32
    4    Q1        FY18  0.23
    5    Q2        FY15  0.61
    6    Q2        FY16  0.51
    7    Q2        FY17  0.45
    8    Q2        FY18  0.54
    9    Q3        FY15  0.78
    10   Q3        FY16  0.79
    11   Q3        FY17  0.81
    12   Q3        FY18  0.84